G++编译器无法识别 C++ 中的枚举类型;
我尝试使用枚举类型和模板类,但我不知道为什么以下代码不起作用:
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要将这些构造函数调用放入
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 returningmlCat<>
thusID
et. al are being interpreted as the types of the arguments.您不能在
class
主体内分配成员(尤其是非静态)变量:当调用
Multilist
对象时,诸如mlCat
之类的成员> 可以在其构造函数中初始化。You can not assign member (especially non-static) variables inside the
class
body:when object of
Multilist
is invoked, members likemlCat<>
can be initialized in its constructor.