幾年前,我正在嘗試重新學習C++後,介紹了一些課程,我有一些基本問題。我嘗試使用朋友功能時出現當前問題。這是我的代碼在2個文件中。函數的多個定義的錯誤
第一:
// fun.cpp
#include <iostream>
using namespace std;
class classA {
friend void funct();
public:
classA(int a=1,int b=2):propa(a),propb(b){cout<<"constructor\n";}
private:
int propa;
int propb;
void outfun(){
cout<<"propa="<<propa<<endl<<"propb="<<propb<<endl;
}
};
void funct(){ // ERROR HERE
cout<<"enter funct"<<endl;
classA tmp(1,2);
tmp.outfun();
cout<<"exit funct"<<endl;
}
二:
// mainfile.cpp
#include <iostream>
#include "fun.cpp"
using namespace std;
int main(int nargin,char* varargin[]) {
cout<<"call funct"<<endl;
funct();
cout<<"exit main"<<endl;
return 0;
}
我正的錯誤是 「本功能的`多個定義()'」。當我將它聲明爲朋友函數時,我使用了錯誤的語法嗎?
此外,它可以與一些連接包裹頭衛士幫助 - 上的#ifndef –
@CarlNorum我的印象是,多個定義錯誤是鏈接錯誤,不是編譯搜索。但也許我錯了。 –
他們是,但頭衛隊沒有任何關係。那麼,除非你在標題中做了一些瘋狂的事情,那就是。 –