作者starmovie (盗族)
看板C_and_CPP
标题[问题] 关於=的多载..
时间Thu Feb 26 22:17:32 2009
以下是程式码
#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(void)
{
cout << "window" << id << ": " << title << endl;
}
~CWin(){delete[] title;}
CWin(const CWin &win)
{
id=win.id;
strcpy(title,win.title);
}
char get_id()
{
return this->id;
}
char get_title()
{
return this->title;
}
};
void operator=(CWin &w1,CWin &w2)
{
w1.get_id()=w2.get_id();
strcpy(w1.get_title(),w2.get_title());
return;
}
int main(void)
{
CWin win1('A',"main window");
CWin win2;
win1.show();
win2.show();
operator=(win1,win2);
win1.show();
system("pause");
return 0;
}
在这个程式码里,我尝试着想要把operator=当成一般函式来写~
但是却无法编译过,我想了很久不知道错在哪边
麻烦各位大大可以帮我看一下~谢谢!!
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 114.44.146.169
1F:推 dendrobium:get_id()回传的是一个暂时的char变数 02/26 22:28
2F:→ dendrobium:对它做assign是不行的 02/26 22:29
3F:→ starmovie:不好意思d大,我不太了解你的意思?? 02/26 22:29
4F:→ starmovie:喔喔...原来是这样 02/26 22:30
5F:→ akasan:get_title的型态也错了 02/26 22:30
6F:→ starmovie:请问一下是哪边错呢..我是想要回传char型态的title 02/26 22:36
7F:推 hylkevin:指标不熟的话建议你用string就好 回传string& 02/26 22:52
8F:→ hylkevin:你的title是char*却要当char回传编译不会过 02/26 22:53
9F:→ starmovie:原来如此...谢谢h大及a大 02/26 23:33
10F:推 aaa12345:改成这样 char & get_id(){} 应该就可以assign了 02/28 02:03