關於friend的宣告 |
答題得分者是:aftcast
|
deepking
一般會員 發表:8 回覆:7 積分:3 註冊:2008-06-03 發送簡訊給我 |
這是一個關於複數的作業
.h檔裡: class{ .... Complex Polar(const double leng, const double arg); //剛剛忘了加這行 friend Complex Polar(const double leng, const double arg); ... } .cpp檔裡: Complex Polar(const double leng, const double arg){省略}; main.cpp裡: b=Polor(6,7); 當我編譯的時候,都會出現 Polar 在此作用欄中尚未宣告 可如上上面,已經宣告... 但是在devc 上能夠順利編譯並且正確 這是編譯器的問題嘛? |
aftcast
站務副站長 發表:81 回覆:1485 積分:1763 註冊:2002-11-21 發送簡訊給我 |
我沒看到你有宣告耶!
也許是你隨手po一段程式碼? 我覺得你class的宣告很怪,不加class的名字,既然不加的情形下也不使用 class{ } obj1,obj2 ,我的意思是指沒有 obj1 或 obj2 之類的。 這不是原來的程式碼吧? 是簡易的表達? 回到主題: .h檔裡: Complex Polar(const double leng, const double arg); //剛剛忘了加這行 //這行應該要放在外面才正確 class{ .... friend Complex Polar(const double leng, const double arg); ... } 然後在main.cpp裡include 上面的.h檔
------
蕭沖 --All ideas are worthless unless implemented-- C++ Builder Delphi Taiwan G+ 社群 http://bit.ly/cbtaiwan |
deepking
一般會員 發表:8 回覆:7 積分:3 註冊:2008-06-03 發送簡訊給我 |
我是把程式碼片斷貼出來而已XD...
呼叫polar要有兩個方法: //a和b都是class,在main.cpp裡 b = a.Polar(5.6, 1.8); b = Polar(6.5, 8.1); //這行就會說Polar尚未宣告 這是教授給的.h檔 class Complex { public: Complex(const double re=0, const double im=0); Complex(const Complex& c); Complex& operator = (const Complex& c); friend Complex Polar(const double leng, const double arg); friend double Norm(const Complex& x); friend double Abs(const Complex& x); friend double Arg(const Complex& x); friend Complex operator (const Complex& x, const Complex& y); friend Complex operator-(const Complex& x, const Complex& y); friend Complex operator*(const Complex& x, const Complex& y); friend Complex operator/(const Complex& x, const Complex& y); friend Complex operator =(Complex& x, const Complex& y); friend Complex operator-=(Complex& x, const Complex& y); friend Complex operator*=(Complex& x, const Complex& y); friend Complex operator/=(Complex& x, const Complex& y); friend bool operator==(const Complex& x, const Complex& y); friend bool operator!=(const Complex& x, const Complex& y); friend ostream& operator<<(ostream& o, const Complex& x); private: double real; double imag; }; |
aftcast
站務副站長 發表:81 回覆:1485 積分:1763 註冊:2002-11-21 發送簡訊給我 |
看了你的程式碼,我終於比較清楚你在做什麼了 ^ ^
那行不用放外面,那是Complex的成員函式(方法),當你使用a.Polar( )時就是用到這個。所以不要去動它了! b = a.Polar(5.6, 1.8); 定義過,但要在這裡宣告,因為xx.cpp與main.cpp是不同的檔案 b = Polar(6.5, 8.1); //這行就是老師要考驗你懂不懂的地方吧??!!
------
蕭沖 --All ideas are worthless unless implemented-- C++ Builder Delphi Taiwan G+ 社群 http://bit.ly/cbtaiwan |
deepking
一般會員 發表:8 回覆:7 積分:3 註冊:2008-06-03 發送簡訊給我 |
|
aftcast
站務副站長 發表:81 回覆:1485 積分:1763 註冊:2002-11-21 發送簡訊給我 |
插在這行b = Polar(6.5, 8.1);
的正上方啊 friend Complex Polar(const double leng, const double arg); //這行不算是宣告,這是告指示Polar這個一般函式為此class的friend 把宣告與定義這二個名詞搞懂,這是很重要的基本功力! ===================引 用 aftcast 文 章=================== 看了你的程式碼,我終於比較清楚你在做什麼了 ^ ^ 那行不用放外面,那是Complex的成員函式(方法),當你使用a.Polar( )時就是用到這個。所以不要去動它了! b = a.Polar(5.6, 1.8); 定義過,但要在這裡宣告,因為xx.cpp與main.cpp是不同的檔案 b = Polar(6.5, 8.1); //這行就是老師要考驗你懂不懂的地方吧??!!
------
蕭沖 --All ideas are worthless unless implemented-- C++ Builder Delphi Taiwan G+ 社群 http://bit.ly/cbtaiwan |
deepking
一般會員 發表:8 回覆:7 積分:3 註冊:2008-06-03 發送簡訊給我 |
大概了解你的意思了︿︿謝謝
要找個時間再把宣告定義的細節再複習一下==|| 另外,為什麼我用DEVC++可以編譯並且執行呢XD... ===================引 用 aftcast 文 章=================== 插在這行b = Polar(6.5, 8.1); 的正上方啊 friend Complex Polar(const double leng, const double arg); //這行不算是宣告,這是告指示Polar這個一般函式為此class的friend 把宣告與定義這二個名詞搞懂,這是很重要的基本功力! ===================引 用 aftcast 文 章=================== 看了你的程式碼,我終於比較清楚你在做什麼了 ^ ^ 那行不用放外面,那是Complex的成員函式(方法),當你使用a.Polar( )時就是用到這個。所以不要去動它了! b = a.Polar(5.6, 1.8); 定義過,但要在這裡宣告,因為xx.cpp與main.cpp是不同的檔案 b = Polar(6.5, 8.1); //這行就是老師要考驗你懂不懂的地方吧??!! |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |