[問題] 還是把完整的問題寫出來好了

看板C_and_CPP (C/C++)作者 (TAKUA)時間16年前 (2010/01/21 09:06), 編輯推噓3(307)
留言10則, 5人參與, 最新討論串1/1
( *[1m *[m 為色碼,可以按 Ctrl+V 預覽會顯示的顏色 ) ( 未必需要依照此格式,文章條理清楚即可 ) 遇到的問題: (題意請描述清楚) "C++ How to program 5ed" 11-17,創造一個 polynomial 的class 希望得到的正確結果: 上一個問題在各位前輩一語點醒之下破關了。 並且也再接再厲做出了operator + 以及 operator- 結果在要overload operator= 的時候出了問題... --------------------以下是我的header file------------------------ class polynomial { friend ostream &operator<< (ostream & output, polynomial & pol) { pol.get_pol(); return output; } friend istream &operator>> (istream & input, polynomial & pol) { pol.set_pol(); return input; } public: polynomial(); ~polynomial(); void set_pol(); void get_pol(); polynomial &operator+ (const polynomial&); polynomial &operator- (const polynomial&); polynomial &operator* (const polynomial&); polynomial &operator+=(const polynomial& poly) { *this = *this + poly; return *this; } polynomial &operator-=(const polynomial& poly) { *this = *this - poly; return *this; } polynomial &operator*=(const polynomial& poly) { *this = *this * poly; return *this; } polynomial &operator= (const polynomial&); private: int terms; int number; int** temp; int** real; void arrange(); }; ------------------------以下為有.CPP檔operator= 附近的段落---------------- polynomial &polynomial::operator= (const polynomial& pol){ delete [] real; terms = pol.terms; real = new int*[terms]; for(int t=0; t<terms; ++t){ real[t] = new int[2]; } for (int t=0; t<terms; ++t){ real[t][0] = pol.real[t][0]; real[t][1] = pol.real[t][1]; } return *this; } 程式跑出來的錯誤結果: `polynomial& polynomial::operator=(const polynomial&)' and `polynomial& polynomial::operator=(const polynomial&)' cannot be overloaded 開發平台: (例: VC++ or gcc/g++ or Dev-C++, Windows or Linux) Dev-C++ 有問題的code: (請善用置底文標色功能) 如上,operator+ operator- 都OK啊... 補充說明: 這是1/25要交的作業...有點急就是了。 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.57.147.96

01/21 09:48, , 1F
因為你動態二維陣列配置是錯的, 其實板上找找一定有的:)
01/21 09:48, 1F

01/21 10:03, , 2F
錯在哪裡呢?我真的是相當外行...恐怕需要高手清楚點出來
01/21 10:03, 2F

01/21 10:03, , 3F
真是抱歉....
01/21 10:03, 3F

01/21 10:06, , 4F
*real = .... 但是 real 根本就不知道是指到哪...
01/21 10:06, 4F

01/21 10:07, , 5F
原來~!在精華區找到了~!感謝各位!
01/21 10:07, 5F

01/21 10:07, , 6F
real = new int* [terms]; //real 指向一個 int* 陣列
01/21 10:07, 6F

01/21 10:08, , 7F
其實在上一個問題的回文裡我就推文說了....orz
01/21 10:08, 7F

01/21 10:56, , 8F
*temp = new int[number]; 這邊也要改
01/21 10:56, 8F
以上感謝各位~! ※ 編輯: takua624 來自: 61.57.147.96 (01/21 11:12)

01/21 11:23, , 9F
.....?突然解決了...沒事了
01/21 11:23, 9F

01/21 13:55, , 10F
眼熟的id
01/21 13:55, 10F
文章代碼(AID): #1BLwY7dx (C_and_CPP)
文章代碼(AID): #1BLwY7dx (C_and_CPP)