線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1390
推到 Plurk!
推到 Facebook!

關於friend的宣告

答題得分者是:aftcast
deepking
一般會員


發表:8
回覆:7
積分:3
註冊:2008-06-03

發送簡訊給我
#1 引用回覆 回覆 發表時間:2008-11-24 22:02:02 IP:140.117.xxx.xxx 未訂閱
這是一個關於複數的作業

.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 上能夠順利編譯並且正確
這是編譯器的問題嘛?
編輯記錄
deepking 重新編輯於 2008-11-24 22:03:15, 註解 無‧
deepking 重新編輯於 2008-11-25 00:09:59, 註解 無‧
deepking 重新編輯於 2008-11-25 00:11:04, 註解 無‧
aftcast
站務副站長


發表:81
回覆:1485
積分:1763
註冊:2002-11-21

發送簡訊給我
#2 引用回覆 回覆 發表時間:2008-11-25 10:10:50 IP:122.120.xxx.xxx 訂閱
我沒看到你有宣告耶!

也許是你隨手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
編輯記錄
aftcast 重新編輯於 2008-11-25 10:14:50, 註解 無‧
aftcast 重新編輯於 2008-11-25 10:15:15, 註解 無‧
aftcast 重新編輯於 2008-11-25 10:19:32, 註解 無‧
deepking
一般會員


發表:8
回覆:7
積分:3
註冊:2008-06-03

發送簡訊給我
#3 引用回覆 回覆 發表時間:2008-11-25 12:12:29 IP:140.117.xxx.xxx 未訂閱
我是把程式碼片斷貼出來而已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;

};
編輯記錄
deepking 重新編輯於 2008-11-25 12:16:39, 註解 無‧
deepking 重新編輯於 2008-11-25 12:18:18, 註解 無‧
deepking 重新編輯於 2008-11-25 12:18:57, 註解 無‧
deepking 重新編輯於 2008-11-25 12:20:04, 註解 無‧
deepking 重新編輯於 2008-11-25 12:24:05, 註解 無‧
aftcast
站務副站長


發表:81
回覆:1485
積分:1763
註冊:2002-11-21

發送簡訊給我
#4 引用回覆 回覆 發表時間:2008-11-25 16:01:18 IP:122.120.xxx.xxx 訂閱
看了你的程式碼,我終於比較清楚你在做什麼了 ^ ^

那行不用放外面,那是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

發送簡訊給我
#5 引用回覆 回覆 發表時間:2008-11-25 19:09:21 IP:140.117.xxx.xxx 未訂閱

===================引 用 aftcast 文 章===================
看了你的程式碼,我終於比較清楚你在做什麼了 ^ ^

那行不用放外面,那是Complex的成員函式(方法),當你使用a.Polar( )時就是用到這個。所以不要去動它了!

b = a.Polar(5.6, 1.8);
定義過,但要在這裡宣告,因為xx.cpp與main.cpp是不同的檔案
b = Polar(6.5, 8.1); //這行就是老師要考驗你懂不懂的地方吧??!!
aftcast
站務副站長


發表:81
回覆:1485
積分:1763
註冊:2002-11-21

發送簡訊給我
#6 引用回覆 回覆 發表時間:2008-11-25 19:16:14 IP:60.248.xxx.xxx 訂閱
插在這行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

發送簡訊給我
#7 引用回覆 回覆 發表時間:2008-11-25 19:25:58 IP:140.117.xxx.xxx 未訂閱
大概了解你的意思了︿︿謝謝
要找個時間再把宣告定義的細節再複習一下==||
另外,為什麼我用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); //這行就是老師要考驗你懂不懂的地方吧??!!
系統時間:2024-05-08 10:55:58
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!