2011-03-22 52 views
0


我有一个类:如何在一个类中定义一个结构?

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; 
    } 

} 

线processTable[i].fptrCntr = 0;给出了一个错误:

systemcall.cc:86: error: expected unqualified-id before '[' token 

我几乎把我所有的头发!任何想法为什么发生这种情况?我甚至把结构放在systemcall.cc文件中,但没用。

谢谢。

回答

4

由于声明的是对象而不是类型,所以在声明processTable之前,您不希望typedef。在将processTable定义为类型之后,然后继续将其用作成员对象,这会使编译器感到困惑。

+0

去除的typedef在这之后是什么情况:在这里,我系统调用* mysyscall =新的系统调用另一个文件(exception.cc);我得到这个错误:exception.cc:57:错误:没有匹配函数调用'systemcall :: systemcall() – infinitloop 2011-03-22 16:29:26

+1

@rashid:是否OpenFile默认可构造?您将需要轻松地初始化'systemcall'的数组成员。 – 2011-03-22 16:30:30

+0

在注释掉OpenFile之后,它就像魅力一般。那么OpenFile有一个构造函数,但我将如何检查它是否是默认可构造的?我对这些术语很陌生。谢谢 – infinitloop 2011-03-22 16:39:23

0

试试这个:

class systemcall 
{ 
    struct ProcessTable 
    { 
     int pid; 
     int fptrCntr; 
     OpenFile openfileptrs[10]; 

    }; 

    ProcessTable processTable[100]; 

    public: 
     //other stuff... 
}; 
相关问题