所以,我有一套相當簡單的模板,我想一起使用,但編譯器一直告訴我B :: a的類型不完整。一切都在宣告着,但它仍然沒有工作...調用對方函數的模板
#include <iostream>
using namespace std;
template <typename T> class A;
template <typename T> class B;
template <typename T>
class A{
public:
void ATestFunction();
void CallBFunction();
protected:
B<T> b;
};
template <typename T>
class B{
public:
void BTestFunction();
void CallAFunction();
protected:
A<T> a;
};
template <typename T>
void A<T>::ATestFunction(){
cout << "A was used for a function call" << endl;
}
template <typename T>
void B<T>::BTestFunction(){
cout << "B was used for a function call" << endl;
}
template <typename T>
void A<T>::CallBFunction(){
b.BTestFunction();
}
template <typename T>
void B<T>::CallAFunction(){
a.ATestFunction();
}
int main()
{
A<int> dragons;
dragons.CallBFunction();
return 0;
}
我問這個,因爲我已經碰到一些困難編程中的一些是互相依賴的(實現二維數組,可以數組類型類可以這樣訪問:[] []),但是這個問題發生了,並且在工作中拋出了一個裝置。我做了這個測試程序,但仍然失敗。我已經在Linux上嘗試了MinGW 4.7.2和GNU g ++,並且每個人都給我提出了同樣的問題。