在C++學習中,使用友元函數時,大多會遇到這樣的問題。比如下面的友元全局函數的使用,就存在問題
//友元全局函數
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
class Test;
void fun(Test & test) {
cout << test.m_strName << endl;
}
class Test {
friend void fun(Test & test);
public:
Test(string name):m_strName(name){}
private:
string m_strName;
};
int main(void) {
Test a("name");
fun(a);
system("pause");
return 0;
}
這裡編譯時就會報錯:Line 8:Test類型未定義。
但是,這就納悶了。我不是在之前寫了class Test;聲明了嗎?
有這樣的感覺是正常的,因為你沒了解一個注意。使用類的聲明,系統隻知道有這樣一個聲明,并不知道有什麼成員。這樣在定義和聲明之間就隻能使用這個類型的引用或者指針
但是在這裡的代碼中第八行卻使用了他的成員。如果把cout << test.m_strName << endl;注釋掉,可以正常編譯.這就能證明這點。
解決方法有兩種:
第一種,把fun全局函數放到後面來。(這種方法适用于友元全局函數)
第二種,是函數的聲明寫在這裡,定義寫在需要使用的類的後面。(這種方法,在友元成員函數的定義時是非常有效的,而且某些情況隻能使用這種方法,例如下面的代碼)
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
class Test2;
class Test1 {
public:
void fun(Test2 & t) {
cout << t.m_strName << endl;
}
};
class Test2 {
friend void Test1::fun(Test2 & t);
public:
Test2(string name): m_strName(name){}
private:
string m_strName;
};
int main() {
Test2 t2("name");
Test1 t1;
t1.fun(t2);
system("pause");
return 0;
}
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
class Test2;
class Test1 {
public:
void fun(Test2 & t);
};
class Test2 {
friend void Test1::fun(Test2 & t);
public:
Test2(string name): m_strName(name){}
private:
string m_strName;
};
void Test1::fun(Test2 & t) {
cout << t.m_strName << endl;
}
int main() {
Test2 t2("name");
Test1 t1;
t1.fun(t2);
system("pause");
return 0;
}