我有一个程序,我正在使用用户指定的测量来计算矩形的面积。我出于特定原因使用类来执行此操作,但我的编译器会生成两个错误...
expression must have class type
left of '.getArea' must have class/struct/union
我该如何解决这个问题?
Rectangle.h
class Rectangle
{
private:
int length;
int width;
int area = length * width;
public:
Rectangle(int l, int w);
int getLength();
void setLength(int l);
int getWidth();
void setWidth(int w);
int getArea();
void setArea(int a);
};
Rectangle.cpp
Rectangle::Rectangle(int l, int w)
{
length = l;
width = w;
}
--some code--
int Rectangle::getArea()
{
return area;
}
void Rectangle::setArea(int a)
{
area = a;
}
Area.cpp
int i, lth, wth;
for (i = 0; i < 3; i++)
{
cout << "Enter your measurements, length first" << endl;
cin >> lth >> wth;
Rectangle rMeasure(int lth, int wth);
cout << "Area of this rectangle is: " << rMeasure.getArea(); //problem code
}
答案 0 :(得分:1)
此
Rectangle rMeasure(int lth, int wth);
是一个函数声明。
看来你的意思是
Rectangle rMeasure(lth, wth);
答案 1 :(得分:0)
您的代码中很少需要更改
1
private:
int length;
int width;
int area;
2
Rectangle::Rectangle(int l, int w)
{
length = l;
width = w;
this->area=length * width;
}
3
for (i = 0; i < 3; i++)
{
cout << "Enter your measurements, length first" << endl;
cin >> lth >> wth;
Rectangle rMeasure(lth, wth);
cout << "Area of this rectangle is: " << rMeasure.getArea();
}