[問題] C++ class的實作問題(解決)

看板C_and_CPP作者 (神汁手)時間10年前 (2015/12/18 06:38), 10年前編輯推噓4(4015)
留言19則, 7人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) code::blocks 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): Error message: undefined reference to code(已經重新寫過): Complex.h: #ifndef COMPLEX_H #define COMPLEX_H class Complex { public: Complex();//default constructor Complex(double, double);//constructor Complex& cadd(Complex&); Complex& csubtract(Complex&); Complex& cmultiply(Complex&); static void print(Complex&); private: double real; double imaginary; };//end class the_complex #endif // COMPLEX_H Complex.cpp: #include<iostream> #include"Complex.h" using namespace std; the_complex::Complex()//default constructor { real = 0; imagunary = 0; } the_complex::Complex(double a,double b)//constructor { real = a; imaginary = b; } Complex& the_complex::cadd(const Complex& a) { (*this).real += a.real; (*this).imaginary += a.imaginary; return *this; } Complex& the_complex::csubtract(Complex& a) { (*this).real -= a.real; (*this).imaginary -= a.imaginary; return *this; } Complex& the_complex::cmultiply(Complex& a) { (*this).real = a.real * (*this).real - (*this).imaginary * a.imaginary; (*this).imaginary = a.imaginary * (*this).real + a.real * (*this).imaginary; return *this; } void the_complex::print(Complex& a) { cout<<a.real<<'+'<<a.imaginary<<'i'<<endl; } main.cpp: #include <iostream> #include "Complex.h" #include <string> using namespace std; int main() { double x1, y1, x2, y2; char s; Complex result; x1 = x2 = y1 = y2 = 0; cout<<"Please enter the real and imaginary number of the first complex:"<<endl; cin>>x1>>y1; Complex e1(x1, y1); cout<<"Please enter the real and imaginary number of the second complex:"<<endl; cin>>x2>>y2; Complex e2(x2, y2); cout<<"Please choose an operation: + or - or *"<<endl; cin>>s; if (s == '+') result = e1.cadd(e2); else if (s == '-') result = e1.csubtract(e2); else if (s == '*') result = e1.cmultiply(e2); else { cout<<"Error input!"<<endl; return 0; } the_complex::print(result); return 0; } 已經解決了....... 我沒有用Add files,而是直接新增檔案 所以對軟體來說我沒有把他們放在同一個project底下...... 感謝C大和m大回答!!! -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.239.250.138 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1450391926.A.013.html ※ 編輯: kiwistar (36.239.250.138), 12/18/2015 07:21:15

12/18 07:15, , 1F
header file裡面函數沒加return type 不知道是不是這原因
12/18 07:15, 1F

12/18 07:17, , 2F
然後程式碼盡量還是貼文字檔會比較好看...
12/18 07:17, 2F
※ 編輯: kiwistar (36.239.250.138), 12/18/2015 07:24:08

12/18 07:24, , 3F
已經改了,謝謝!!然後問題也變了
12/18 07:24, 3F

12/18 07:51, , 4F
the_complex改成Complex
12/18 07:51, 4F

12/18 09:31, , 5F
你是說左上角的那個選單嗎?那個怎麼改?
12/18 09:31, 5F

12/18 11:09, , 6F
先把新的code貼上來。照你的寫法,print應該要是static
12/18 11:09, 6F

12/18 12:26, , 7F
這邊已經是新的code了
12/18 12:26, 7F

12/18 12:27, , 8F
我有發現一個地方,不知道是不是問題所在
12/18 12:27, 8F

12/18 12:27, , 9F
就是我的function沒有從object來呼叫,但我是定義在clas
12/18 12:27, 9F

12/18 12:28, , 10F
裡面。改成把函數(add, multiply...)改為static可解決嗎
12/18 12:28, 10F

12/18 12:30, , 11F
你的the_complex沒有改成Complex
12/18 12:30, 11F
他不讓我改...還是有其他地方可以設定? 左上角那個框框

12/18 12:31, , 12F
除此之外,.h裡面不要打using namespace std;
12/18 12:31, 12F

12/18 12:33, , 13F
仔細看了一下你的add、subtract,一種方法是改成static
12/18 12:33, 13F

12/18 12:34, , 14F
一種方法是operator overloading+friend
12/18 12:34, 14F
我也想過用operator overloding的方法 可是這個題目出現在class的章節 我猜他應該認為這用member function做就可 改static可以解決ISO C++ forbids declaration of XXX with no type這個error嗎?

12/18 12:38, , 15F
另外,standard裡面有<complex>,你確定要自己做一個?
12/18 12:38, 15F
這是課後練習的作業TwT ※ 編輯: kiwistar (223.142.239.56), 12/18/2015 12:54:44 ※ 編輯: kiwistar (223.142.239.56), 12/18/2015 13:53:42 ※ 編輯: kiwistar (223.142.239.56), 12/18/2015 13:55:29 ※ 編輯: kiwistar (223.142.239.56), 12/18/2015 13:56:54 ※ 編輯: kiwistar (223.142.239.56), 12/18/2015 14:39:55

12/19 10:49, , 16F
幹嘛要一堆(*this),覺得很煩嗎
12/19 10:49, 16F

12/19 12:10, , 17F
this根本多餘
12/19 12:10, 17F

12/19 12:31, , 18F
說不定是從C#帶過來的習慣
12/19 12:31, 18F

12/19 19:04, , 19F
要用 this 不是不行, 但為什麼不用 -> 呢...
12/19 19:04, 19F
文章代碼(AID): #1MSpbs0J (C_and_CPP)