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

发布于 2024-10-31 18:42:22 字数 1594 浏览 2 评论 0原文

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

#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: 错误:“ID” 不是一个类型
../src/multilist.cpp:110:错误: “职业”不是一种类型
../src/multilist.cpp:111:错误: “LOCATION”不是类型

I tried to use enumerated type and template class, but i have no idea why the following code doesn't work:

#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 return an error in the 'CATEGORY DEFINITION' part:

../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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奶气 2024-11-07 18:42:22

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

You need to put those constructor calls in the constructor of Multilist: Multilist(...) : catId(ID), catOccupation(OCCUPATION), ... and remove them from the declarations. Your current usage looks like you are trying to declare functions returning mlCat<> thus ID et. al are being interpreted as the types of the arguments.

南巷近海 2024-11-07 18:42:22

您不能在 class 主体内分配成员(尤其是非静态)变量:

class A {
  int i = 0; // error: member assignment not allowed in current C++ standard
  int j(2);  // error: compiler thinks 'j' is a function; with argument type as 2
  int k;     // ok
};

当调用 Multilist 对象时,诸如 mlCat 之类的成员> 可以在其构造函数中初始化。

You can not assign member (especially non-static) variables inside the class body:

class A {
  int i = 0; // error: member assignment not allowed in current C++ standard
  int j(2);  // error: compiler thinks 'j' is a function; with argument type as 2
  int k;     // ok
};

when object of Multilist is invoked, members like mlCat<> can be initialized in its constructor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文