请C++各位高手帮帮忙
来源:百度知道 编辑:UC知道 时间:2024/07/02 15:34:10
为何总说有错误,
#include<iostream.h>
class Rectangle{
private:
typedef float LengthType;
typedef float AreaType;
LengthType width,height;
public:
void SetWidth(LengthType newWidth);
void SetHeight(LengthType newHeight);
AreaType Area();
LengthType Perimeter();
};
void Rectangle::SetWidth(LengthType newWidth)
{
width=newWidth;
}
void Rectangle::SetHeight(LengthType newHeight)
{
height=newHeight;
}
Rectangle::AreaType Rectangle::Area()
{
return heigth*width;
}
Rectangle::LengthType::Rectangle::Perimeter()
{
return 2*(width+height);
}
void main()
{
Rectangle rect;
rect.SetWidth(45);
rect.SetHeight(54.2);
cout<<"The area of rectangle is"<<rect.Area()<<endl;
cout<<"The perimeter of rectangle is"<<rect.Perimeter
#include<iostream.h>
class Rectangle{
private:
typedef float LengthType;
typedef float AreaType;
LengthType width,height;
public:
void SetWidth(LengthType newWidth);
void SetHeight(LengthType newHeight);
AreaType Area();
LengthType Perimeter();
};
void Rectangle::SetWidth(LengthType newWidth)
{
width=newWidth;
}
void Rectangle::SetHeight(LengthType newHeight)
{
height=newHeight;
}
Rectangle::AreaType Rectangle::Area()
{
return heigth*width;
}
Rectangle::LengthType::Rectangle::Perimeter()
{
return 2*(width+height);
}
void main()
{
Rectangle rect;
rect.SetWidth(45);
rect.SetHeight(54.2);
cout<<"The area of rectangle is"<<rect.Area()<<endl;
cout<<"The perimeter of rectangle is"<<rect.Perimeter
一般用VC创建类的时候有默认的构造函数和析构函数,如果编译还有初始化问题那就把类中的变量定义及函数初始化一下,指定个空值或初始值。
再就是看错误提示,没有错误提示我也不好说,毕竟你贴的只是一个片段,
最常犯的错误就是初始化问题和忘记包含头文件,还有就是一些小的语法错误如拉了个分号什么的。
你把错误提示粘上来看看
Rectangle rect;
对象没有初始化
------------------补充回答---------------------
Rectangle& rect = Rectangle();
你应该编写构造函数来完成默认的初始化工作