c++ - G++ 编译器无法识别 C++ 中的枚举类型

标签 c++ templates enums enumeration

我尝试使用枚举类型和模板类,但我不知道为什么下面的代码不起作用:

#include <stdio.h>
#include <string.h>

class Multilist {
    //TYPE DEFINITION
    struct mlNode;                      //forward declarations
    struct dlNode;
    template <class T> struct mlCat;

    typedef char tstr[21];              //our string type
    enum catIterator {ID=0, OCCUPATION,LOCATION};

    struct mlNode { //Multilist Node
        tstr name; int id;
        mlNode* p[3];   //next nodes
        mlNode* n[3];   //previous nodes
        dlNode* h[3];   //h[i] point to the entry in category i
        mlNode(tstr sName, int sId) {
            strcpy(name,sName); id=sId; //nOccupation=snOccupation; nId=snId; nLocation=snLocation;
        }
    };

    // One class to rule them all =)
    template <class T> struct mlCat {   //Multilist Category
        catIterator c;
        mlCat(catIterator tc): head(0), c(tc) {};

        struct dlNode { //list node
            dlNode *next;
            T data; mlNode *link; //data & link to the record in the db
            dlNode(T d, mlNode *l, dlNode *n=0): link(l),data(d),next(n) {};
        } *head;

    };

    //CATEGORY DEFINITION
    mlCat<int>  catId(ID);
    mlCat<tstr> catOccupation(OCCUPATION);
    mlCat<tstr> catLocation(LOCATION);


};

int main(int narg, char * arg[]) {
    return 0;
}

Eclipse 在“类别定义”部分返回错误:

../src/multilist.cpp:109: error: ‘ID’ is not a type
../src/multilist.cpp:110: error: ‘OCCUPATION’ is not a type
../src/multilist.cpp:111: error: ‘LOCATION’ is not a type

最佳答案

您需要将这些构造函数调用放在 Multilist 的构造函数中: Multilist(...) : catId(ID), catOccupation(OCCUPATION), ...并将它们从声明中删除。您当前的用法看起来像是在尝试声明返回 mlCat<> 的函数因此ID等。 al 被解释为参数的类型。

关于c++ - G++ 编译器无法识别 C++ 中的枚举类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5607613/

相关文章:

c++ - 在 C++ 中,有两个结构引用彼此的变量

c++ - 反转 vector 并打印它

c# - 显示枚举描述而不是名称

.net - 获取枚举值的 GetFields 方法

c++ - BlackJack 卡 vector c++/随机数

c++ - 临时对象的复制省略

c++ - AT 命令列表为 char * 数组

c++ - 问题重构奇怪地重复出现的模板模式

c++ - ModelResetter RAII 对象

c++ - 实例化类模板时,是否也实例化了成员模板声明?