这是教材上用来讲类的组合的一个例子,我基本上跟书上写的一样,代码这样:大虾们请指点一下小弟
#include<iostream>
#include<cmath>
using namespace std;
class Point
{
private:
double x;
double y;
public:
Point(double x,double y){x=x;y=y;cout<<"Point类构造函数被调用"<<endl;}
Point(Point & p);
~Point(){}
double getx(){}
double gety();
}
Point::Point(Point & p)//////////////////////error C2533: 'Point::Point' : constructors not allowed a return type
{
x=p.x;
y=p.y;
cout<<"Point类拷贝构造函数被调用"<<endl;
}
double Point::getx()
{
return x;
}
double Point::gety()
{
return y;
}
class Line
{
private:
Point p1,p2;
double leng;
public:
Line(Point p1,Point p2);
Line(Line &);
~Line(){}
double getl();
}
Line::Line(Point p1,Point p2):p1(p1),p2(p2)//////////////////////////////////同上
{
double xl=p1.getx()-p2.getx();
double yl=p1.gety()-p2.gety();
leng=sqrt(xl*xl+yl*yl);
cout<<"Line类构造函数被调用"<<endl;
}
Line::Line(Line & li):p1(li.p1),p2(li.p2)
{
leng=li.leng;
cout<<"Line类拷贝构造函数被调用"<<endl;
}
void main()
{
Point p1(3,4);
Point p2(7,8);
Line l1(p1,p2);
Line l2(l1);
cout<<"The length of l1 is:"<<l1.getl()<<endl;
cout<<"The length of l2 is:"<<l2.getl()<<endl;
return ;
}
这是全部的编译报错信息:
--------------------Configuration: Line - Win32 Debug--------------------
Compiling...
Line.cpp
D:\My Program\Line\Line.cpp(18) : error C2533: 'Point::Point' : constructors not allowed a return type
D:\My Program\Line\Line.cpp(43) : error C2533: 'Line::Line' : constructors not allowed a return type
D:\My Program\Line\Line.cpp(44) : error C2264: 'Point::Point' : error in function definition or declaration; function not called
D:\My Program\Line\Line.cpp(44) : error C2264: 'Point::Point' : error in function definition or declaration; function not called
D:\My Program\Line\Line.cpp(51) : error C2264: 'Point::Point' : error in function definition or declaration; function not called
D:\My Program\Line\Line.cpp(51) : error C2264: 'Point::Point' : error in function definition or declaration; function not called
D:\My Program\Line\Line.cpp(59) : error C2264: 'Line::Line' : error in function definition or declaration; function not called
Error executing cl.exe.
Line.obj - 7 error(s), 0 warning(s)