當兩個類要互相用對方定義的型別時應該怎么寫啊
我有一個類A和類B,A中的成員變數a的型別為B中的b_type,B中的成員變數b的型別為A中的a_type,要怎么寫才能不報錯
class A {
public:
using a_type = int;
B::b_type a;
}
class B {
public:
using b_type = int;
A::a_type b;
}
uj5u.com熱心網友回復:
class B;//預定義
class A {
public:
using a_type = int;
B::b_type a;
}
class B {
public:
using b_type = int;
A::a_type b;
}
uj5u.com熱心網友回復:
這個貌似沒法解決B::b_type????
uj5u.com熱心網友回復:
1.缺少前置宣告:class B;2.前置宣告只能作為指標或參考,不能定義類的物件,也不能呼叫物件中的方法
解決方法:將宣告和實作分離,可以在對應的實作中定義B::b_type型別的a變數
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/82101.html
標籤:C++ 語言
上一篇:求解答關于指標和malloc的題
下一篇:關于陣列問題
