2016-08-02 67 views
1
class a { 
public: 
void f2(b * elem); 
}; 

class b { 
public: 
void f1(a * elem); 
}; 

這裏會出現問題。帶有成員函數的兩個類將指針指向另一個類的對象

void f2(b * elem); 

我如何以這樣的方式,我可以使用功能f2聲明b類?

+2

轉發聲明 – Jovasa

+0

是的,你可以。有什麼問題? – songyuanyao

+0

在我的簡單代碼中有一個錯誤的語法錯誤:標識符b;編譯器不知道誰是b,那麼我如何使它工作? –

回答

2

告訴編譯器有一個類a和b。但是不要告訴他/她們是什麼樣子:)這是可能的,因爲你使用指針。它只是編譯器的整數。之後,您可以定義類的所有功能,編譯器會很高興知道它們現在的樣子。

class a; // tell the compiler there is a class a 
class b; // tell the compiler there is a class b 

// real implementation of class a 
class a { 
    public: 
     void f2(b * elem); 
}; 

// real implementation of class b 
class b { 
    public: 
     void f1(a * elem); 
}; 
相關問題