我不希望这是我的第一篇文章,但我在这里输了。在试图编译我的程序时(我应该只是简单地找到一个矩形的区域和边界),我总是收到这个错误。这是我的头文件。表达必须有一个类的类型
#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