计算三角形面积时出错?

时间:2017-09-13 22:14:57

标签: c++

我正在研究一个程序,它读取三角形三角形的底边和高度,然后报告该区域。这是我到目前为止所做的:

#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  

我在这里做错了什么?

2 个答案:

答案 0 :(得分:4)

问题在于以下几点:

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;

你可以安全地删除这两行;他们实际上并没有做任何事情而且会导致你看到的一些错误。

答案 1 :(得分: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;

}