C++ G++;编译器不';无法识别C+中的枚举类型+;
我尝试使用枚举类型和模板类,但不知道为什么以下代码不起作用:C++ G++;编译器不';无法识别C+中的枚举类型+;,c++,templates,enums,enumeration,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;
#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;
}
#包括
#包括
类多重列表{
//类型定义
struct mlNode;//转发声明
结构dlNode;
模板结构mlCat;
typedef char tstr[21];//我们的字符串类型
枚举计算器{ID=0,职业,位置};
结构mlNode{//多列表节点
tstr名称;int id;
mlNode*p[3];//下一个节点
mlNode*n[3];//以前的节点
dlNode*h[3];//h[i]指向类别i中的条目
mlNode(tstr sName,int sId){
strcpy(名称,sName);id=sId;//nOccupation=snoccuption;nId=snId;nLocation=snLocation;
}
};
//一个类来管理所有类=)
模板结构mlCat{//多列表类别
阳离子滴定剂c;
mlCat(catIterator-tc):头(0),c(tc){};
结构dlNode{//列表节点
dlNode*下一步;
T data;mlNode*link;//数据&链接到数据库中的记录
dlNode(td,mlNode*l,dlNode*n=0):链路(l),数据(d),下一个(n){};
}*团长;
};
//类别定义
mlCat catId(ID);
职业;
mlCat catLocation(位置);
};
int main(int narg,char*arg[]{
返回0;
}
Eclipse在“类别定义”部分返回错误:
../src/multilist.cpp:109:错误:“ID”
不是一种类型../src/multilist.cpp:110:错误: “职业”不是一种类型
../src/multilist.cpp:111:错误: “位置”不是类型
您需要将这些构造函数调用放入
Multilist
:Multilist(…):catId(ID)、catocculation(occulation),…
的构造函数中,并从声明中删除它们。您当前的用法看起来像是试图声明返回mlCat
的函数,因此ID
等被解释为参数的类型。您需要将这些构造函数调用放入多列表的构造函数中:多列表(…):catId(ID)、catOccupation(OCCUPATION),…并将其从声明中删除。您当前的用法看起来像是试图声明返回mlCat
的函数,因此ID
等被解释为参数的类型。您不能在类
主体内分配成员(尤其是非静态)变量:
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
之类的成员。不能在类
主体内分配成员(尤其是非静态)变量:
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
之类的成员