看板 EE_DSnP 關於我們 聯絡資訊
Compiler跟我說Int main()裡面的Circle::getPerimeter()沒有matching function 但我不會修正,想請大家指點一下,感謝! 這是我的程式碼 #include <iostream> using namespace std; class Circle {public: float getPerimeter(float a,int b,int c) {cin >> a >> b >> c ; cout << 2*a*3.14; } float getArea(float a,int b,int c) {cin >> a >> b >> c ; cout << 3.14*a*a ; } }; class Square {public: float getArea() {cin >> d >> e >> f ; cout << d*d; } float getPerimeter() {cin >> d >> e >> f ; cout << 4*d; } private: float d; int e,f; }; int main() { float r1, r2; int x1, y1, x2, y2; cout << "Please enter the radius and center coordinates " << "for the circle (r/x/y): "; cin >> r1 >> x1 >> y1; cout << "Please enter the side length and center coordinates " << "for the Square (s/x/y): "; cin >> r2 >> x2 >> y2; Circle c1; Square s2; float p1 = c1.getPerimeter(), p2 = s2.getPerimeter(); if (p1 > p2) cout << "Circle has larger perimeter (" << p1 << ") than " << "Square(“ << p2 << “)!!" << endl; else cout << "Circle has smaller perimeter (“ << p1 << “) than " << "Square(“ << p2 << “)!!" << endl; float a1 = c1.getArea(), a2 = s2.getArea(); if (a1 > a2) cout << "Circle has larger area (“ << a1 << “) than " << "Square(“ << a2 << “)!!" << endl; else cout << "Circle has smaller area (“ << a1 << “) than " << "Square(“ << a2 << “)!!" << endl; return 0; system ("pause"); } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.236.234
AlanLee76083:float getarea 沒有return值 10/22 20:01
ilway25:"getxxx" function 的意思你有點搞錯了~ 10/22 20:17
ilway25:恩還有constructor 10/22 20:18
INsoarDEEP:可以麻煩大大解釋一下嗎?我真的不太懂 10/22 20:21
ilway25:去翻翻老師上課講義弄懂每一行吧..這很難用一兩行字道盡.. 10/22 20:24
ilway25:還有你把老師給的程式碼改了... 10/22 20:26
ally01202:getXXX的函數基本上沒有輸入值 10/22 20:46
ally01202:所以要用constructer 直接從class裡把變數拿來運算 10/22 20:47
nicksxz:補充一點 相同的data不需要重複輸入 10/22 21:12
nicksxz:所以有些cin是不需要的 10/22 21:16
nicksxz:另外就是cout和return是不一樣的 要分清楚 10/22 21:18
ric2k1:嗯, 除了以上各位的回答之外, 簡單的說, 就是你 10/22 21:32
ric2k1:Circle::getPerimeter() 不用傳參數, 而是用 constructor 10/22 21:34
ric2k1:被呼叫時存入的 data members 就好了 (好像跟上面的人說的 10/22 21:34
ric2k1:一樣...) 10/22 21:35
INsoarDEEP:感謝大家的指點,我成功COMPILE,程式也能順利跑了, 10/22 23:37
INsoarDEEP:原來是有好多基本概念都不清楚,我要加把勁了 10/22 23:39