2013-02-22 41 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; 
相關問題