我尝试使用枚举类型和模板类,但我不知道为什么下面的代码不起作用:
#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/