//////////////////////TestClass.h
#ifndef _TEST_H_
#define _TEST_H_
class TestClass2;
class TestClass1
{
private:
int i;
public:
TestClass1()
{
i = 10;
}
void ChangePrivate(TestClass2& test,int change_i);
};
class TestClass2
{
private:
int i;
public:
TestClass2()
{
i = 20;
}
friend void TestClass1::ChangePrivate(TestClass2& test,int change_i);
};
#endif
///////////////////////////TestClass.cpp
#include "stdafx.h"
#include "TestClass.h"
void TestClass1::ChangePrivate(TestClass2& test,int change_i)
{
test.i = change_i;
}
/////////////////////Main.cpp
#include "TestClass.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
TestClass1 test1;
TestClass2 test2;
test1.ChangePrivate(test2,1);
getchar();
getchar();
return 0;
}
现在我不明白的是TestClass.cpp里的
void TestClass1::ChangePrivate(TestClass2& test,int change_i)
{
test.i = change_i;
}
我如果放在TestClass.h里面 就link错误,如果哪位仁兄能指导下不胜感激,错误提示如下:
Generating Code...
Linking...
TsetClass.obj : error LNK2005: "public: void __thiscall TestClass1::ChangePrivate(class TestClass2 &,int)" (?ChangePrivate@TestClass1@@QAEXAAVTestClass2@@H@Z) already defined in thinking test.obj
E:\随便\thinking test\Debug\thinking test.exe : fatal error LNK1169: one or more multiply defined symbols found
Build log was saved at "file://e:\随便\thinking test\thinking test\Debug\BuildLog.htm"
thinking test - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========