2014-12-20 32 views
0

这是代码;错误:使用未识别的类型'顶点'

class Vertex; 

class CPD 
{ 
private: 
    width; 

public: 
    void initialize() 
    { . 
     . 
     . 
    } 

    void updateTable(LinkedList<Vertex*>* parents) 
    { 
     node<Vertex *> *ptr = parents->getHead(); 
     int W = 1; 
     while (ptr) 
     { 
      W *= ((ptr->data)->getStates())->getSize(); 
      ptr = ptr->next; 
     } 
     width = W; 
     initialize(); 
    } 
}; 

但是,我得到的第一条语句while循环中的“使用未定义类型的‘顶点’”的错误,虽然我已经给了一类顶点原型开头。一些帮助将不胜感激,谢谢。

+4

你需要顶点的完整定义,不只是一个前向声明。 – kec

+0

好吧,'顶点'类本身使用'CPD'类,所以生病回到原来的一个... –

+1

'updateTable()'不应该内联。将其移到实现文件中。那么你会好起来的。 – kec

回答

0

只要给出了Vertex的前向声明,编译器就不知道关于这个类及其成员的任何信息。它怎么可能?

尽管如此,您不需要这些CPD申报的细节。编译器只需知道用于理解函数签名的类Vertex 就存在

这样,您可以避免Vertex和CPD的相互依赖性。正如@kec指出的,解决方案是将updateTable()的定义移动到另一个文件中,其中包含Vertex的完整定义。

文件Vertex.hpp:

class Vertex { 
    // declaration here 
}; 

文件CPD.hpp:

class Vertex; 

class CPD { 
    void updateTable(LinkedList<Vertex*>* parents); 
    // ... 
}; 

文件CPD.cpp:

#include "Vertex.hpp" 

void CPD::updateTable(LinkedList<Vertex*>* parents){ 
    // definition here 
}