2017-09-13 235 views
-1

我正在研究一個程序,該程序讀取三角形的三角形的底部和高度,然後報告該區域。這是我到目前爲止有:計算三角形面積的錯誤?

#include<iostream> 
using namespace std; 

// a simple triangle class with a base and a height 

class Triangle { 

public:  
    double setbase(double x) { //was void but changed it to double 
     base = x; 
    }  
    double setheight(double y) { 
     height = y; 
    } 
    double getbase() { 
     return base; 
    } 
    double getheight() { 
     return height; 
    } 

private: 
    double base, height; 
};  


double area(double, double);  

int main() {  

    Triangle isoscoles1; 
    isoscoles1.setbase; 
    isoscoles1.setheight; 


    cin >> isoscoles1.setheight; 
    cin >> isoscoles1.setbase; 
    double x = isoscoles1.getbase; 
    double y = isoscoles1.getheight; 

    cout << "Triangle Area=" << area(x,y) << endl; 
    system("pause>nul"); 
} 


double area(double x, double y) { 
    double a; 
    a = (x*y)/2;  

    return a; 
} 

這是錯誤它給了我: -

Severity Code Description Project File Line Suppression State 
Error C3867 'Triangle::setbase': non-standard syntax; use '&' to create a pointer to member Project1 c:\users\ku\desktop\test\project1\main.cpp 50 
Error C3867 'Triangle::setheight': non-standard syntax; use '&' to create a pointer to member Project1 c:\users\ku\desktop\test\project1\main.cpp 51 
Error C2679 binary '>>': no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion) Project1 c:\users\ku\desktop\test\project1\main.cpp 54 
Error C2679 binary '>>': no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion) Project1 c:\users\ku\desktop\test\project1\main.cpp 55 
Error C3867 'Triangle::getbase': non-standard syntax; use '&' to create a pointer to member Project1 c:\users\ku\desktop\test\project1\main.cpp 56 
Error C3867 'Triangle::getheight': non-standard syntax; use '&' to create a pointer to member Project1 c:\users\ku\desktop\test\project1\main.cpp 57 

我在做什麼錯在這裏?

+3

考慮從[好書]學習(https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list ),而不是隨機編碼。 –

+0

不是很有用。但感謝您訪問 – LogomonicLearning

+0

爲什麼它沒有用?你所犯的錯誤如此微不足道,關於這些錯誤的語法應該在任何好書的第一章中討論。通過建議讀一個,我可以防止你在將來製造更多類似的問題。 –

回答

3

的問題是,在這些線路:

cin >> isoscoles1.setheight; 
cin >> isoscoles1.setbase; 

記住setheightsetbase是成員函數,而不是實際的變量。這就像寫這樣的:

void doSomething(double arg) { 
    ... 
} 

cin >> doSomething; // Error - can't read into a function! 

在上述情況下,你真的做這樣的事情:

double value; 
cin >> value; 
doSomething(value); 

這個分離出來,從調用該函數讀取值。

基於此,看看您是否可以考慮如何使用類似的解決方案來解決自己的問題。


這裏還有一個問題,正如評論中指出的那樣。看看這些行:

isoscoles1.setbase; 
isoscoles1.setheight; 

回想一下我們的doSomething函數。你通常會有這樣的代碼行嗎?

doSomething; 

您可以安全地刪除這兩行;他們實際上沒有做任何事情,並且導致了一些你看到的錯誤。

+0

錯誤出現在_attempted_函數'getbase' /'getheight'調用中的缺失圓括號中。 –

+0

@AlgirdasPreidžius好的。答案已更新! – templatetypedef

+0

謝謝,這澄清了一些事情。下面的代碼現在完美地工作 – LogomonicLearning

0

這很好。

class Triangle { public: void setbase(double x) { //was void but changed it to double base = x; }

void setheight(double y) { 
    height = y; 
} 
double getbase() { 
    return base; 
} 
double getheight() { 
    return height; 
} 

private: double base; double height; };

double area(double, double);

int main() {

Triangle isoscoles1; 
double x; 
double y; 
cin >> x >> y; 

isoscoles1.setheight(y); 
isoscoles1.setbase(x); 
double b = isoscoles1.getbase(); 
double h = isoscoles1.getheight(); 

cout << "Triangle Area=" << area(x,y) << endl; 
system("pause>nul"); 
return 0; 

}

double area(double x, double y) { double a; a = (x*y)/2;

return a; 

}