[問題] 怎麼在一個class裡call另一個class?

看板C_and_CPP (C/C++)作者 (sherry)時間13年前 (2013/04/27 12:44), 編輯推噓0(001)
留言1則, 1人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) Linux 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) <iostream> 問題(Question): 怎麼從class A call class B讓他執行B的constructor? 餵入的資料(Input): ./a.out 預期的正確結果(Expected Output): size of a=5 錯誤結果(Wrong Output): 0427_01.cpp: In constructor ‘A::A(int)’: 0427_01.cpp:9:8: error: ‘B’ was not declared in this scope 程式碼(Code):(請善用置底文網頁, 記得排版) #include<iostream> using namespace std; class A{ public: A(int a) { B(a); } }; class B { friend class A; public: B(int size) { cout<<"size of a="<<size<<endl; } }; int main(){ A a(5); return 0; } 補充說明(Supplement): -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.116.123.175

04/27 13:55, , 1F
class B沒有在class A前宣告
04/27 13:55, 1F
文章代碼(AID): #1HUrUzV4 (C_and_CPP)
文章代碼(AID): #1HUrUzV4 (C_and_CPP)