[問題] class Vector失敗

看板C_and_CPP作者 (追風箏的孩子)時間14年前 (2010/06/26 16:52), 編輯推噓0(002)
留言2則, 2人參與, 最新討論串1/1
( *[1m *[m 為色碼,可以按 Ctrl+V 預覽會顯示的顏色 ) ( 未必需要依照此格式,文章條理清楚即可 ) 遇到的問題: (題意請描述清楚) 第28行(in>>b.x>>b.y;)錯誤,error C2679:二元運算子'>>':找不到使用右方運算元型別 'const double'的運算子(或是沒有可接受的轉換) 希望得到的正確結果: 輸入a向量,內建b向量,跑出a+b,a-b,a dot b 程式跑出來的錯誤結果: 無法編譯 開發平台: (例: VC++ or gcc/g++ or Dev-C++, Windows or Linux) VC++2008 有問題的code: (請善用置底文標色功能) in>>b.x>>b.y; 補充說明: 我的程式碼: #include <iostream> using namespace std; class Vector{ private: double x,y; public: Vector(){x=4.0,y=5.0;} Vector(double a,double b){x=a,y=b;} Vector operator+(const Vector &b) {return Vector(x+b.x,y+b.y);} Vector operator-(const Vector &b) {return Vector(x-b.x,y-b.y);} double operator*(const Vector &b) {return (x*b.x+y*b.y);} friend ostream& operator<<(ostream &out,const Vector &b) { out<<"("<<b.x<<","<<b.y<<")"<<endl; return out; } friend istream& operator>>(istream &in,const Vector &b) { in>>b.x>>b.y; return in; } }; int main() { Vector a,b(4.0,5.0),c,d; double e; cout<<"輸入向量\n"; cin>>a; c=a+b; d=a-b; e=a*b; cout<<"a="<<a<<"b="<<b<<"c="<<c<<"d="<<d<<"e="<<e<<endl; return 0; } 感謝大家幫忙debug -- ◢███◣ ◢██◣ 洨派氣功!! ▄▆▄ ▄▆ ◥◥◥◥◥◥ / ◥ ▅◥ //// ˊ ˋ □︵□ ◣◢◣ ◢◣ ◢◣ ~───~ /// 啊~ ◥◣ ◥◣ ψqazsugf ⊙ ⊙ / ◥◣ ◥◣ ▃▁ . . ▂▂ ◤ ◥◤ ◥◤▅▃ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.241.109

06/26 17:05, , 1F
26行去掉const 你都const還要改變它當然不行
06/26 17:05, 1F

06/26 18:00, , 2F
OK了,感謝
06/26 18:00, 2F
文章代碼(AID): #1C9R_abG (C_and_CPP)