0

我不希望这是我的第一篇文章,但我在这里不知所措。尝试编译我的程序时,我不断收到此错误(它应该只是找到矩形的面积和周长。)这是我的头文件。

#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();
}
4

3 回答 3

2

您遇到了被称为最令人烦恼的解析。

Rectangle rectangle1();
Rectangle rectangle2();

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

Rectangle rectangle1;
Rectangle rectangle2;

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

于 2013-02-22T06:00:54.287 回答
1

Rectangle::Perimeter() 和 Rectangle::Area() 是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();

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

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;你的函数做什么的那行,是接受一个float名为 的参数Lngth,然后检查Wdth(私有变量)是否在范围内,如果是,则声明一个float名为的新局部变量wdth并将其设置为与 相同的值Wdth

该函数根本不初始化私有变量Wdth。你的setWdth功能也是如此。你也应该修复这些。

于 2013-02-22T06:01:10.603 回答
0

这个:

 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;
于 2013-02-22T06:01:25.020 回答