问题描述
- 这代码错在哪,我真的找不出来
-
#include
using namespace std;
class Rectangle
{
private:
int width,length;
public:
Rectangle(int a,int b)
{
width=a;
length=b;
}
void set_rectangle(int w,int l)
{width=w;length=l;}
int area();
int perimeter();
}
int Rectangle::area()
{
return(width*length);
}
int Rectangle::perimeter()
{
return(2*(width+length));
}
int main()
{
Rectangle r(12,4);
cout<<"this rectangle's area is "<<r.area()<<",perimeter is "<<r.perimeter();
r.set_rectangle(2,4);
cout<<"the new rectangle's area is "<<r.area()<<",perimeter is "<<r.perimeter();
return 0;
}
解决方案
#include <iostream>
using namespace std;
class Rectangle
{
private:
int width,length;
public:
Rectangle(int a,int b)
{
width=a;
length=b;
}
void set_rectangle(int w,int l)
{width=w;length=l;}
int area();
int perimeter();
};
int Rectangle::area()
{
return(width*length);
}
int Rectangle::perimeter()
{
return(2*(width+length));
}
int main()
{
Rectangle r(12,4);
cout<<"this rectangle's area is "<<r.area()<<",perimeter is "<<r.perimeter();
r.set_rectangle(2,4);
cout<<"the new rectangle's area is "<<r.area()<<",perimeter is "<<r.perimeter();
return 0;
}
解决方案二:
- #include 这个后面没有头文件
- 类定义没有结束符 ;
解决方案三:
类定义结束时应该加上分号
解决方案四:
http://codepad.org/uywc4qKZ
this rectangle's area is 48,perimeter is 32the new rectangle's area is 8,perimeter is 12
解决方案五:
class 类名
{
public:
//公共的行为或属性
private:
//公共的行为或属性
};
你的类定义缺少最后的“;”
解决方案六:
就照大家的意见办吧。写代码时的小遗漏。
解决方案七:
要注意细节,头文件,“;”遗漏
解决方案八:
缺少分好,,,,小问题啊
时间: 2024-08-06 06:54:07