C++ 类“不是模板类型”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1590688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Class 'is not a template type'
提问by Scott
What does this error mean?
这个错误是什么意思?
Generic.h:25: error: 'Generic' is not a template type
Here's Generic.
这是通用的。
template <class T>
class Generic: public QObject, public CFG, public virtual Evaluator {
Q_OBJECT
std::string key_;
std::vector<std::string> layouts_;
std::vector<std::string> static_widgets_;
std::map<std::string, std::vector<widget_template> > widget_templates_;
std::map<std::string, Widget *> widgets_;
int type_;
LCDWrapper *wrapper_;
protected:
LCDText *lcdText_;
public:
Generic(Json::Value *config, int type);
~Generic();
void CFGSetup(std::string key);
void BuildLayouts();
void StartLayout();
int GetType() { return type_; }
//T *GetLCD() { return lcd_; }
LCDText *GetLCDText() { return lcdText_; }
virtual void Connect(){};
virtual void SetupDevice(){};
std::map<std::string, Widget *> Widgets();
std::string CFG_Key();
LCDWrapper *GetWrapper() { return wrapper_; }
};
Is the problem that it subclasses other classes? I tried an experiment testing that theory, but it didn't produce this error.
问题是它继承了其他类吗?我尝试了一个实验来测试该理论,但没有产生此错误。
Edit: Ok, so a couple of you guys pointed out I could be forward declaring Generic elsewhere without making it a template class. That's true. And when I make it a template, I get another error.
编辑:好的,所以你们中的一些人指出我可以在其他地方提前声明 Generic 而不使其成为模板类。确实如此。当我把它做成模板时,我又收到了另一个错误。
Property.h:15: error: ISO C++ forbids declaration of 'Generic' with no type
Property.h:15: 错误:ISO C++ 禁止声明没有类型的“通用”
template <class T>
class Generic;
class Property : public CFG {
Generic *visitor; // line 15
bool is_valid;
QScriptValue result;
Json::Value *expression;
public:
Property(const Property &prop);
Property(Generic *v, Json::Value *section, std::string name, Json::Value *defval);
~Property();
bool Valid();
int Eval();
double P2N();
int P2INT();
std::string P2S();
void SetValue(Json::Value val);
Property operator=(Property prop);
};
采纳答案by Gaston
I'm not sure if this is your problem, but you can'tsubclass QObject with a template class.
我不确定这是否是您的问题,但是您不能使用模板类对 QObject 进行子类化。
回答by razlebe
Take a look at this similar questionelsewhere on SO. Are you by any chance forward-declaring Generic somewhere, and not as a templated class?
在 SO 的其他地方看看这个类似的问题。您是否有机会在某处预先声明泛型,而不是作为模板类?
EDIT: In answer to your second error...
编辑:回答你的第二个错误......
@Steve Guidi has solved this problem in his comment elsewhere on this page. Now that you've consistently declared Generic as a templated class, line 15 of your Property.h is illegal because you're using Generic in an untemplated form.
@Steve Guidi 在本页其他地方的评论中解决了这个问题。既然您一直将 Generic 声明为模板类,那么您的 Property.h 的第 15 行是非法的,因为您以非模板形式使用 Generic。
You need to specify the specialization you're using on line 15, e.g.
您需要在第 15 行指定您使用的专业化,例如
template <class T>
class Generic;
class Property : public CFG {
Generic<int> *visitor; // specialised use of Generic
bool is_valid;
QScriptValue result;
Json::Value *expression;
public:
Property(const Property &prop);
Property(Generic *v, Json::Value *section, std::string name, Json::Value *defval);
~Property();
bool Valid();
int Eval();
double P2N();
int P2INT();
std::string P2S();
void SetValue(Json::Value val);
Property operator=(Property prop);
};
回答by stijn
might be that Generic is already defined somewhere else?
可能是泛型已经在其他地方定义了吗?
回答by David Seiler
Check to see whether the #include
directive immediately above Generic
has a closing ;
on its class definition. I saw that error once, and that was the cause.
检查#include
上面的指令是否在其类定义上Generic
有一个结束;
。我曾经看到过那个错误,这就是原因。
回答by Gaston
Your problem is that you are defining visitor of type Generic with no template param.
您的问题是您正在定义没有模板参数的 Generic 类型的访问者。
When you forward declared the class as Generic then at line 15 the compiler found the declaration. But when you changed the declaration with template then the class Generic is no longer found.
当您向前将类声明为 Generic 时,编译器在第 15 行找到了该声明。但是当您使用模板更改声明时,将不再找到类 Generic。
So, you should do something like:
因此,您应该执行以下操作:
template <class T>
class Generic;
class Property : public CFG {
Generic<SomeType> *visitor; // line 15
or
或者
template <class T>
class Generic;
template <class T>
class Property : public CFG {
Generic<T> *visitor; // line 15
or something like that.
或类似的东西。
回答by TemporalBeing
Aside from the issue that you are not defining the template class correctly (thus your error message), you cannot use a Q_OBJECT in a template class. See http://doc.trolltech.com/qq/qq15-academic.htmlfor details.
除了您没有正确定义模板类的问题(因此是您的错误消息),您不能在模板类中使用 Q_OBJECT。详见http://doc.trolltech.com/qq/qq15-academic.html。
回答by C?t?lin Piti?
Is that error generated by C++ compiler, or by Qt MOC? It might be that you can't actually apply Qt MOC on a template class.
该错误是由 C++ 编译器还是由 Qt MOC 生成的?可能是您实际上无法在模板类上应用 Qt MOC。