类'不是模板类型'

Sco*_*ott 16 c++ templates

这个错误是什么意思?

Generic.h:25: error: 'Generic' is not a template type
Run Code Online (Sandbox Code Playgroud)

这是通用的.

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_; }

};
Run Code Online (Sandbox Code Playgroud)

它是否是其他类的子类的问题?我尝试过测试该理论的实验,但它没有产生这个错误.

编辑:好的,所以你们几个人指出我可以在其他地方声明Generic,而不是将它作为模板类.确实如此.当我把它作为模板时,我得到另一个错误.

Property.h:15:错误:ISO C++禁止声明没有类型的'Generic'

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);
};
Run Code Online (Sandbox Code Playgroud)

raz*_*ebe 22

在SO的其他地方看看这个类似的问题.你有没有机会在某个地方向前宣布Generic,而不是一个模板化的类?

编辑:回答你的第二个错误......

@Steve Guidi在本页其他地方的评论中解决了这个问题.既然你一直将Generic声明为模板化类,那么你的Property.h的第15行是非法的,因为你是以非模板形式使用Generic.

您需要在第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);
};
Run Code Online (Sandbox Code Playgroud)


Gas*_*ton 8

我不确定这是否是你的问题,但你不能用模板类子类化QObject.

这里有更多相关信息.


sti*_*ijn 5

可能是Generic已经在其他地方定义了?

  • @Scott:使用模板指令保持存根declration.编译器现在告诉你,你正在尝试使用`Generic`作为非泛型类型(即Generic*visitor) - 你也需要专门化这些实例. (2认同)