表达式必须具有类类型

Expression must have a class type

本文关键字:类型 表达式      更新时间:2023-10-16

我不想这是我的第一篇帖子,但我在这里不知所措。当我试图编译我的程序(应该只是简单地找到矩形的面积和周长)时,我一直会遇到这个错误。这是我的头文件。

#include <iostream>
using namespace std;
class Rectangle
{
public:
   Rectangle(float Lngth=1, float Wdth = 1);
   void setLngth(float Lngth);
   void setWdth(float Wdth);
   float getLngth(float Lngth);
    float getWdth(float Wdth);
    void Perimeter(float lngth, float wdth);
    void Area(float lngth, float wdth);
private:
    float Lngth;
    float Wdth;
};

这是我的.cpp文件。

#include <iostream>
using namespace std;
#include "RealRectangle.h" // Employee class definition

 Rectangle::Rectangle(float Lngth, float Wdth)
 {
&Rectangle::setLngth;
&Rectangle::setWdth;
 }
 void Rectangle::setLngth(float Lngth)
 {
      if((Wdth > 0.0) && (Wdth < 20.0))
       float wdth = Wdth;
        else
            cout<<"Invalid Width."<<endl;
 }
 float Rectangle::getLngth(float Lngth)
 {
     return Lngth;
 }
void Rectangle::setWdth(float Wdth)
{
     if((Wdth > 0.0) && (Wdth < 20.0))
       float wdth = Wdth;
        else
            cout<<"Invalid Width."<<endl;
}
float Rectangle::getWdth(float Wdth)
{
    return Wdth;
}
void Rectangle::Perimeter(float lngth, float wdth)
    {
        cout<<"The Perimeter is "<<(2*(lngth + wdth));
    }
void Rectangle::Area(float lngth, float wdth)
    {
        cout<<"The Area is "<<(lngth * wdth);
    }

这就是我不断遇到错误的地方。编译器告诉我添加一个符号来创建指针,就像我在.cpp中所做的那样。但这本身就产生了另一个错误。等等。我不确定我做错了什么。错误出现在第10行和第11行。

#include <iostream>
using namespace std;
#include "RealRectangle.h" 

int main()
{
    Rectangle rectangle1();
    Rectangle rectangle2();
    cout<<rectangle1.Perimeter();
    cout<<rectangle2.Area();
}

您遇到了被称为最麻烦的解析。

Rectangle rectangle1();
Rectangle rectangle2();

声明了两个函数,而不是两个对象。进行

Rectangle rectangle1;
Rectangle rectangle2;

此外,您可能应该将这些&Rectangle::setLngth更改为函数调用。

矩形::周长()和矩形::面积()为void类型。他们什么都不回。然而,您正试图使用它们不存在的返回值,并将其传递给cout

修改这两个函数,使它们返回一个值:

float Rectangle::Perimeter(float lngth, float wdth)
{
        return 2 * (lngth + wdth);
}
float Rectangle::Area(float lngth, float wdth)
{
        return lngth * wdth;
}

或者修改main()函数以简单地调用这些函数,因为现在它们已经打印到cout:

int main()
{
    Rectangle rectangle1;
    Rectangle rectangle2;
    rectangle1.Perimeter();
    rectangle2.Area();
}

但你仍然有一个问题;这两个函数目前采用长度和宽度参数,我认为这不是您想要的。看起来您想要的是获得矩形对象的周长和面积。所以你应该使用类变量来计算。因此,省略这些参数,转而使用您的私人数据成员:

float Rectangle::Perimeter()
{
        return 2 * (Lngth + Wdth);
}
float Rectangle::Area()
{
        return Lngth * Wdth;
}

不要忘记更新头文件中类声明中的函数签名,而不仅仅是cpp文件中的实现。

此外,您的构造函数没有正确地委托初始化工作。函数调用的形式是function(arguments),而不是&function。所以你需要做:

Rectangle::Rectangle(float Lngth, float Wdth)
{
    setLngth(Lngth);
    setWdth(Wdth);
}

最后,Rectangle对象的声明被误解为函数原型:

Rectangle rectangle1();
Rectangle rectangle2();

编译器认为rectangle1rectangle2是不带参数并返回矩形的函数。你应该省略括号:

Rectangle rectangle1;
Rectangle rectangle2;

我们还没有完成(天哪,这个程序有多少错误:-p)。您的setLngthsetWdth功能未按预期工作:

void Rectangle::setLngth(float Lngth)
{
    if((Wdth > 0.0) && (Wdth < 20.0))
        float wdth = Wdth;
    else
        cout<<"Invalid Width."<<endl;
}

仔细看一下。特别是说float wdth = Wdth;的那一行。你的函数所做的是,取一个名为Lngthfloat参数,然后检查Wdth(私有变量)是否在范围内,如果是,则声明一个新的本地float变量wdth,并将其设置为与Wdth相同的值。

该函数根本不初始化私有变量WdthsetWdth函数也是如此。你也应该修理这些。

这:

 Rectangle::Rectangle(float Lngth, float Wdth)
 {
&Rectangle::setLngth;
&Rectangle::setWdth;
 }

应该是这样的:

Rectangle::Rectangle(float Lngth, float Wdth)
{
    setLngth(Lngth);
    setWdth(Wdth);
}

这个:

Rectangle rectangle1();
Rectangle rectangle2();

应该是这样的:

Rectangle rectangle1;
Rectangle rectangle2;
相关文章: