模板和派生类定义:错误:"myClass"不是类,名称空间或枚举 [英] template and derived class definition : error: 'myClass' is not a class, namespace, or enumeration
问题描述
我正在尝试学习C ++中的模板,并且我有以下代码:
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?..
我使用 clang ++ -Wall -Werror -Wextra -c 进行编译.
什么可能导致此错误?
推荐答案
您需要为其指定模板参数,因为 myClass
是一个类模板.
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>() {
// ^^^
}
BTW::std :: stack< T>()
似乎是多余的.
BTW: : std::stack<T>()
seems to be redundant.
这篇关于模板和派生类定义:错误:"myClass"不是类,名称空间或枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!