C++ 如何在类中定义结构?
C++ 如何在类中定义结构?,c++,class,data-structures,structure,C++,Class,Data Structures,Structure,我有一门课: class systemcall { typedef struct { int pid; int fptrCntr; OpenFile openfileptrs[10]; }processTable[100]; public: //other stuff... } 我有一个会员功能 /* this function initializes the process table. *
我有一门课:
class systemcall
{
typedef struct
{
int pid;
int fptrCntr;
OpenFile openfileptrs[10];
}processTable[100];
public:
//other stuff...
}
我有一个会员功能
/* this function initializes the process table. */
void systemcall::initpTable()
{
int i = 0;
for ( i=0; i<100; i++ ) {
processTable[i].fptrCntr = 0;
}
}
我几乎把头发都拔了!!!知道为什么会这样吗?我甚至将该结构放在systemcall.cc文件中,但没有任何用处
谢谢。您不希望在声明
processTable
之前使用typedef
,因为您声明的是对象,而不是类型。将processTable
定义为类型后,继续将其用作成员对象,这会混淆编译器。您不希望在processTable
声明之前使用typedef
,因为您声明的是对象,而不是类型。将processTable
定义为类型后,继续将其用作成员对象,这会混淆编译器。尝试以下操作:
class systemcall
{
struct ProcessTable
{
int pid;
int fptrCntr;
OpenFile openfileptrs[10];
};
ProcessTable processTable[100];
public:
//other stuff...
};
试试这个:
class systemcall
{
struct ProcessTable
{
int pid;
int fptrCntr;
OpenFile openfileptrs[10];
};
ProcessTable processTable[100];
public:
//other stuff...
};
删除typedef后,会发生这样的情况:在另一个文件(exception.cc)中,我有systemcall*mysyscall=newsystemcall;我得到了这个错误:exception.cc:57:error:调用'systemcall::systemcall()@rashid:OpenFile是否可以构造默认值没有匹配的函数?您需要它来轻松初始化
systemcall
的数组成员。OpenFile有一个构造函数,但是我如何检查它是否是默认的可构造的呢?我不熟悉这些术语。感谢you@rashid:Default constructible只是指在删除typedef后,在OpenFile():/*初始值设定项stuff*/{}
中不接受任何参数的构造函数。情况如下:在另一个文件(exception.cc)中,我有systemcall*mysyscall=new systemcall;我得到了这个错误:exception.cc:57:error:调用'systemcall::systemcall()@rashid:OpenFile是否可以构造默认值没有匹配的函数?您需要它来轻松初始化systemcall
的数组成员。OpenFile有一个构造函数,但是我如何检查它是否是默认的可构造的呢?我不熟悉这些术语。感谢you@rashid:Default constructible只是指在OpenFile():/*初始值设定项stuff*/{}