开发平台(Platform): (Ex: VC++, GCC, Linux, ...)
DEV C++
错误结果(Wrong Output):
编译器出现"void operator=(CWin&, CWin&)' must be a nonstatic member function"
的错误
程式码(Code):(请善用置底文网页, 记得排版)
#include<iostream>
#include<cstdlib>
using namespace std;
class CWin
{
private:
char id,*title;
public:
CWin(char i='D',char *text="Default window"):id(i)
{
title=new char[50];
strcpy(title,text);
}
void set_data(char i,char *text)
{
id=i;
strcpy(title,text);
}
void show()
{
cout<<"Window "<<id<<": "<<title<<endl;
}
~CWin()
{
delete [] title;
}
CWin(const CWin &win)
{
id=win.id;
strcpy(title,win.title);
}
friend void operator=(CWin &win1,CWin &win2);//在类别里宣告
};
void operator=(CWin &win1,CWin &win2)///类别外面定义
{
win1.id=win2.id;
strcpy(win1.title,win2.title);
}
int main()
{
CWin win1('A',"Main window");
CWin win2;
win1.show();
win2.show();
operator=(win1,win2);
cout<<endl<<"设定 win1=win2之后..."<<endl;
win1.show();
win2.show();
win1.set_data('B',"hello window");
cout<<endl<<"更改win1的资料成员后..."<<endl;
win1.show();
win2.show();
system("pause");
return 0;
}
请问一下是哪边出现了问题@@?? 谢谢大家