2013-02-22 98 views
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(); 
} 
+1

请发布确切的错误消息,包括行号。 – 2013-02-22 05:59:38

+0

您应该习惯于不在文件中使用'using namespace std;'。它可能会导致命名空间污染。 – ChiefTwoPencils 2013-02-22 06:01:23

回答

2

你已经遇到了被称为最令人头疼的解析。

Rectangle rectangle1(); 
Rectangle rectangle2(); 

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

Rectangle rectangle1; 
Rectangle rectangle2; 

另外,你应该改变那些&Rectangle::setLngth函数调用。

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(); 

编译器认为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;这行代码的功能是,取float的参数Lngth,然后检查Wdth(私有变量)是否在范围内,如果是,则声明一个新的本地float变量,并将其设置为与Wdth相同的值。

该函数根本不会初始化私有变量WdthsetWdth功能也一样。你也应该解决这些问题。

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; 
相关问题