我不希望這是我的第一篇文章,但我在這裏輸了。在試圖編譯我的程序時(我應該只是簡單地找到一個矩形的區域和邊界),我總是收到這個錯誤。這是我的頭文件。表達必須有一個類的類型
#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();
}
請發佈確切的錯誤消息,包括行號。 – 2013-02-22 05:59:38
您應該習慣於不在文件中使用'using namespace std;'。它可能會導致命名空間污染。 – ChiefTwoPencils 2013-02-22 06:01:23