2017-09-13 1 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; 

}

관련 문제