2011-02-01 80 views
0

使用Visual C++ 2010我有類似下面的代碼:C++編譯器告訴我一個類型無法識別

文件A.hpp:

... 
#include "R.hpp" 
... 
class A; // forward declaration because the APtr_t is used inside the A class too. 
typedef boost::shared_ptr<A> APtr_t; 
... 
class A { 
... 
    void some_method() { 
     ... 
     R::get()->mehod(); // singleton ;) 
     ... 
    } 
... 
}; 

文件R.hpp:

... 
#include "A.hpp" 
... 

class R { 
... 
APtr_t method(); 
... 
}; 

Visual C++編輯器說它沒問題(沒有標記錯誤),但編譯項目時,它充當APtr_t未定義。 它顯示了這樣的錯誤:

error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 

的事情是,這個問題只在R.hpp文件正在發生的事情,我想......

你有什麼想法? 這很令人困惑: -/

在此先感謝。

+4

我假定線`APtr_t()的方法:用分號,而不是一個完整的結腸`結束? – templatetypedef 2011-02-01 22:16:24

+2

你需要發佈更多的代碼 - 最好是整個標題。 – Puppy 2011-02-01 22:21:54

回答

4

我的心理調試技巧猜測A.hpp包括R.hpp而且你的頭文件有適當的包括守衛。在這種情況下,包含鏈看起來像blah.cppA.hppR.hppA.hpp (include guard prevents inclusion)。所以它從來沒有在R.hpp之內看到A.hpp的內容。您需要使用其中一種標準方法來移除循環依賴。

0

我認爲這是一個頭文件包含循環,意思是A包含B,B包含A. 通常這會引入這樣的問題。在你的cpp文件中,不管你首先包含哪一個,無論順序如何,它都會報告這樣的問題。 解決方案是不使用循環包括。

我想你沒有任何cpp文件。那麼也許你可以引入另一個hpp文件type.hpp,它純粹定義了類接口但沒有實現,然後在A.hpp和R.hpp中,你可以編寫你的成員函數代碼。

type.hpp

class A; // forward declaration because the APtr_t is used inside the A class too. 
typedef boost::shared_ptr<A> APtr_t; 
... 
class A { 
... 
void some_method(); 
... 
}; 

class R { 
... 
APtr_t method(); 
... 
}; 


a.hpp 
#include "type.hpp" 
void A::some_method() { 
    ... 
    R::get()->mehod(); // singleton ;) 
    ... 
} 


r.hpp 
#include "type.hpp" 
....  
相關問題