| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 809 人关注过本帖
标题:[求助]c++基础问题
只看楼主 加入收藏
cl_zdl
Rank: 1
等 级:新手上路
威 望:1
帖 子:71
专家分:0
注 册:2006-10-11
收藏
 问题点数:0 回复次数:5 
[求助]c++基础问题

请大家帮我看看,错误有些多,希望大家能帮我改改.谢谢!
#include<iostream>
using namespace std;

class Rectangle{
private:Line line1;

public:
Rectangle(Line a):line1(a){}
int mianji(){int s=line1.length1*line1.length2;
return s;}
};

class Line{
private :Point p1,p2;
int length1;
int length2;
public:Line (Point a,Point b):p1(a),p2(b)
{length1=p1.x-p2.x;
length2=p1.y-p2.y;}

Line (Line &p){length1=p.length1;
length2=p.length2;}
};

class Point {
private:int x;
int y;
public:Point (int a ,int b){x=a;y=b;}
Point (Point &p){x=p.x;y=p.y;}
};
void main(){
Point m1(10,20),m2(5,10);
Line p1(m1,m2);
Rectangle rectangle(p1);
cout<<"面积是:"<<rectangle.mianji()<<endl;
}
错误调试:
--Configuration: 11 - Win32 Debug--------------------
Compiling...
11.cpp
D:\学习文件\CL\4-9\11\11.cpp(5) : error C2146: syntax error : missing ';' before identifier 'line1'
D:\学习文件\CL\4-9\11\11.cpp(5) : error C2501: 'Line' : missing storage-class or type specifiers
D:\学习文件\CL\4-9\11\11.cpp(5) : error C2501: 'line1' : missing storage-class or type specifiers
D:\学习文件\CL\4-9\11\11.cpp(8) : error C2629: unexpected 'class Rectangle ('
D:\学习文件\CL\4-9\11\11.cpp(8) : error C2334: unexpected token(s) preceding ':'; skipping apparent function body
D:\学习文件\CL\4-9\11\11.cpp(14) : error C2146: syntax error : missing ';' before identifier 'p1'
D:\学习文件\CL\4-9\11\11.cpp(14) : error C2501: 'Point' : missing storage-class or type specifiers
D:\学习文件\CL\4-9\11\11.cpp(14) : error C2501: 'p1' : missing storage-class or type specifiers
D:\学习文件\CL\4-9\11\11.cpp(14) : error C2501: 'p2' : missing storage-class or type specifiers
D:\学习文件\CL\4-9\11\11.cpp(17) : error C2629: unexpected 'class Line ('
D:\学习文件\CL\4-9\11\11.cpp(17) : error C2334: unexpected token(s) preceding ':'; skipping apparent function body
D:\学习文件\CL\4-9\11\11.cpp(32) : error C2660: 'Line::Line' : function does not take 2 parameters
D:\学习文件\CL\4-9\11\11.cpp(33) : error C2440: 'initializing' : cannot convert from 'class Line' to 'class Rectangle'
No constructor could take the source type, or constructor overload resolution was ambiguous
执行 cl.exe 时出错.

11.obj - 1 error(s), 0 warning(s)

搜索更多相关主题的帖子: 基础 
2006-10-28 08:02
maoguoqing
Rank: 6Rank: 6
来 自:重庆
等 级:贵宾
威 望:28
帖 子:2980
专家分:19
注 册:2005-12-5
收藏
得分:0 

错误不少,主要有3,1:类声明的顺序,2:一个类试图访问另一个类的私有成员,3:没有缺省的构造函数,那么向private:Point p1,p2;这样定义对象就不能成功

#include<iostream>
using namespace std;

class Point {
private:int x;
int y;
public:Point(){}
Point (int a ,int b){x=a;y=b;}
Point (Point &p){x=p.x;y=p.y;}
friend class Line;
};


class Line{
private:Point p1,p2;
int length1;
int length2;
public:Line (Point a,Point b):p1(a),p2(b)
{length1=p1.x-p2.x;
length2=p1.y-p2.y;}

Line (Line &p){length1=p.length1;
length2=p.length2;}
friend class Rectangle;

};

class Rectangle{
private:Line line1;

public:
Rectangle(Line a):line1(a){}
int mianji(){int s=line1.length1*line1.length2;
return s;}
};


void main(){
Point m1(10,20),m2(5,10);
Line p1(m1,m2);
Rectangle rectangle(p1);
cout<<"Ãæ»ýÊÇ:"<<rectangle.mianji()<<endl;
}


天行健,君子以自强不息!!QQ:68660681
2006-10-28 09:25
cl_zdl
Rank: 1
等 级:新手上路
威 望:1
帖 子:71
专家分:0
注 册:2006-10-11
收藏
得分:0 

很感谢你的帮忙,但我还有点不明白的为什么要给point 类加个缺省参的构造函数呢?

2006-10-29 07:24
cl_zdl
Rank: 1
等 级:新手上路
威 望:1
帖 子:71
专家分:0
注 册:2006-10-11
收藏
得分:0 
还有Line类里没缺省的构造函数,为什么Rectangle类中就能Line line1 这样声明呢?
2006-10-29 07:32
litcatyx
Rank: 1
等 级:新手上路
威 望:1
帖 子:151
专家分:0
注 册:2006-10-27
收藏
得分:0 
Why
以下是引用cl_zdl在2006-10-29 7:32:18的发言:
还有Line类里没缺省的构造函数,为什么Rectangle类中就能Line line1 这样声明呢?

[此贴子已经被作者于2006-10-29 10:44:31编辑过]


2006-10-29 10:43
litcatyx
Rank: 1
等 级:新手上路
威 望:1
帖 子:151
专家分:0
注 册:2006-10-27
收藏
得分:0 
因为Rectangle(const Line& a):line1(a){},调用的是Line(Line& p),而不是缺省构造函数。
还有main()的返回值类型为int;
另外拷贝构造函数形参最好声明为const引用,在你的程序中没有必要定义拷贝构造函数,编译器生成的就足够了。

2006-10-29 10:44
快速回复:[求助]c++基础问题
数据加载中...
 
   



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

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