-1

I'm working on a program that reads the base and height of a triangle of a triangle and then reports the area. Here's what I have so far:

#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;
}

This is the error it gives me:-

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  

What am I doing wrong here?

user4581301
  • 33,082
  • 7
  • 33
  • 54
Blake K Akiti
  • 173
  • 1
  • 3
  • 9

2 Answers2

4

The issue is in these lines:

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

Remember that setheight and setbase are member functions, not actual variables. This would be like writing something like this:

void doSomething(double arg) {
    ...
}

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

In the above case, you'd really do something like this:

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

This separates out reading the value from calling the function.

Based on that, see if you can think about how you'd use an analogous solution to solve your own problem.


There's another issue here, as was pointed out in the comments. Take a look at these lines:

isoscoles1.setbase;
isoscoles1.setheight;

Think back to our doSomething function. Would you normally have a line of code like this?

doSomething;

You can safely delete those two lines; they don't actually do anything and they're causing some of the errors you're seeing.

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
0

This works perfectly.

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;

}

Blake K Akiti
  • 173
  • 1
  • 3
  • 9