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

vmonteco picture vmonteco · Jan 15, 2016 · Viewed 20.9k times · Source

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?

Answer

songyuanyao picture songyuanyao · Jan 15, 2016

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.