| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 486 人关注过本帖
标题:难道又是编译器的问题???
只看楼主 加入收藏
lululau
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2005-12-13
收藏
 问题点数:0 回复次数:3 
难道又是编译器的问题???

这是教材上用来讲类的组合的一个例子,我基本上跟书上写的一样,代码这样:大虾们请指点一下小弟

#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)

搜索更多相关主题的帖子: 编译 
2006-04-09 19:49
lululau
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2005-12-13
收藏
得分:0 
唉,太丢人了,连这个最基本的问题都忘了,不应该不应该啊,大家别笑话俺呀

好好学习 天天向上
2006-04-09 20:19
wtuguw
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2006-4-5
收藏
得分:0 

俺也是新手

2006-04-10 19:35
gototheworld
Rank: 1
等 级:新手上路
帖 子:218
专家分:0
注 册:2006-3-24
收藏
得分:0 

#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;
}
double Line::getl()
{
return leng;
}
int 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;
system("pause");
return 0;

}


路漫漫其修远兮 吾将上下而求索
2006-04-10 19:46
快速回复:难道又是编译器的问题???
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016732 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved