[問題] 怎麼在一個class裡call另一個class?
開發平台(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
04/27 13:55, 1F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章