49.obj - 7 error(s), 0 warning(s)?为什么会这样?

#include <iostream>
using namespace std;
class Rect;

//类:坐标
class Position
{
private:
int x,y;
public:
Position(int xx=0,int yy=0)
{
x=xx;
y=yy;
}
Position(Position &P)
{
x=P.x;
y=P.y;
}
int Getx()
{
return x;
}
int Gety()
{
return y;
}
}

//类:矩形
class Rect
{
private:
Postion dl,ur;
int area;
public:
Rect(Position dlp,Position urp):dl(dlp),ur(urp)
{
area=(ur.Getx()-dl.Getx())*(ur.Gety()-dl.Gety());
}
~Rect(Rect &R):dl(R.dl),ur(R.ur)
{
area=R.area;
}
int GetArea()
{
return area;
}
}

//主函数
void main()
{
Position dl1(0,0);
Position ur1(4,5);
Rect r1(dl1,ur1);
cout<<r1.GetArea<<endl;

}

--------------------Configuration: 49 - Win32 Debug--------------------
Compiling...
49.cpp
D:\Program Files\Microsoft Visual Studio\MyProjects\49\49.cpp(33) : error C2236: unexpected 'class' 'Rect'
D:\Program Files\Microsoft Visual Studio\MyProjects\49\49.cpp(33) : error C2143: syntax error : missing ';' before '{'
D:\Program Files\Microsoft Visual Studio\MyProjects\49\49.cpp(33) : error C2447: missing function header (old-style formal list?)
D:\Program Files\Microsoft Visual Studio\MyProjects\49\49.cpp(59) : error C2079: 'r1' uses undefined class 'Rect'
D:\Program Files\Microsoft Visual Studio\MyProjects\49\49.cpp(59) : error C2078: too many initializers
D:\Program Files\Microsoft Visual Studio\MyProjects\49\49.cpp(59) : error C2440: 'initializing' : cannot convert from 'class Position' to 'int'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
D:\Program Files\Microsoft Visual Studio\MyProjects\49\49.cpp(60) : error C2228: left of '.GetArea' must have class/struct/union type
Error executing cl.exe.

49.obj - 7 error(s), 0 warning(s)

慕容3067478
浏览 289回答 1
1回答

月关宝盒

给,已经都改好了,具体的请看注释:#include <iostream>using namespace std;class Rect;//类:坐标class Position{private:int x,y;public:Position(int xx=0,int yy=0){x=xx;y=yy;}Position(Position &P){x=P.x;y=P.y;}int Getx(){return x;}int Gety(){return y;}};//class定义最后要加分号//类:矩形class Rect{private:Position dl,ur; //Postion改为Positionint area;public:Rect(Position dlp,Position urp):dl(dlp),ur(urp){area=(ur.Getx()-dl.Getx())*(ur.Gety()-dl.Gety());}~Rect(void) //析构函数没有参数{//area=R.area; //一般析构函数不进行任何处理}int GetArea(){return area;}};//class定义最后要加分号//主函数void main(){Position dl1(0,0);Position ur1(4,5);Rect r1(dl1,ur1);cout<<r1.GetArea<<endl;}
打开App,查看更多内容
随时随地看视频慕课网APP