Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

template and derived class definition : error: 'myClass' is not a class, namespace, or enumeration

I'm trying to learn templates in C++ and I have the following code :

#include <stack>

template<typename T>
class   myClass : public std::stack<T>{
public:
    myClass(void);
    myClass(myClass const & src);
    virtual ~myClass(void);
    myClass &   operator=(myClass const & rhs);
};

template<typename T>
myClass::myClass(void) : std::stack<T>(){
}

But I can't figure out why I get the following when I try to compile :

test.cpp:17:1: error: 'myClass' is not a class, namespace, or enumeration
myClass::myClass(void) : std::stack<T>(){
^
test.cpp:8:9: note: 'myClass' declared here
class   myClass : public std::stack<T>{
        ^
1 error generated.

It looks like the definition of the function causes the error, but I don't know why I get this error, it looks OK to me (even if I guess it's not really OK), just a syntax error perhaps?..

I compile with clang++ -Wall -Werror -Wextra -c.

What could cause this error?

like image 810
vmonteco Avatar asked Jan 15 '16 02:01

vmonteco


1 Answers

You need to specify the template parameter for it, since myClass is a class template.

template<typename T>
myClass<T>::myClass(void) : std::stack<T>() {
//     ^^^
}

LIVE


BTW: : std::stack<T>() seems to be redundant.

like image 121
songyuanyao Avatar answered Sep 25 '22 19:09

songyuanyao