简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM