2014-12-20 35 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 
}