C2659 Resolution Questions

Asked 2 years ago, Updated 2 years ago, 24 views

#include <iostream>
using namespace std;

class Rectangle {
int width, height;
public:
Rectangle();
Rectangle(int, int);
int getWidth() { return width; }
int getHeight() { return height; }
int area(void) { return (width*height); }
};
Rectangle::Rectangle() {
width = 5;
height = 5;
}
Rectangle::Rectangle(int a, int b) {
width = a;
height = b;
}
int main() {
Rectangle r;
Rectangle *p;
p = &r;
r.getWidth = 2;
r.getHeight = 4;

cout << p->getWidth() << endl;
cout << p->getHeight() << endl;
}

If I do this, it says that the function was used as the left operand, what should I do?

c++

2022-09-22 17:56

1 Answers

This is because we did a substitution operation on the function name in the code below.

r.getWidth = 2;
r.getHeight = 4;

If you want to set a value for that variable, you can use the method of creating and invoking the variable setting function as shown below.

void setWidth(int v) { width = v; }
void setHeight(int v) { height = v; }
//...
r.setWidth(2);
r.setHeight(4);
// ...


2022-09-22 17:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.