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

#include <iostream>
using namespace std;
class Point
{
private:
int x;
int y;
public:
Point(){};
Point (int a,int b)
{
x=a;
y=b;
}
~Point(){};
void set(int a,int b);
int get_x(){return x;}
int get_y(){return y;}
void print() const
{
cout<<"("<<x<<","<<y<<")\n";
}
friend Point operator + ( Point &a,Point &b);
};

void Point::set(int a,int b)
{
x=a;
y=b;
}

point operator + ( Point &a,Point &b)
{
return (a.x+b.x,a.y+b.y);
}

void main()
{
Point a,b;
a.set(3,2);
b.set(1,5);
(a+b).print();
//add(a,b).print();

}

:\C++\c++作业\3\d.cpp(85) : fatal error C1001: INTERNAL COMPILER ERROR
(compiler file 'msc1.cpp', line 1786)
Please choose the Technical Support command on the Visual C++
Help menu, or open the Technical Support help file for more information
执行 cl.exe 时出错.

请问:这个是什么意思??

搜索更多相关主题的帖子: 重载 
2006-12-08 13:30
hnzzc
Rank: 1
等 级:新手上路
帖 子:86
专家分:0
注 册:2006-4-26
收藏
得分:0 
已经发现错误了,
将#include <iostream>
using namespace std;
改成#include<iostream.h>
就可以了.
#include <iostream>
using namespace std;和#include<iostream.h>有什么区别
2006-12-08 15:35
zhanghuan_10
Rank: 1
等 级:新手上路
威 望:2
帖 子:751
专家分:0
注 册:2006-10-25
收藏
得分:0 
main.cpp:32: error: expected constructor, destructor, or type conversion before "operator"
main.cpp:32: error: expected `,' or `;' before "operator"
:: === Build finished: 2 errors, 0 warnings ===
我用我的编译器编译之后还是有错误!具体如上!呵呵!不知道为什么!

该学习了。。。
2006-12-08 16:12
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
你的程序可以是这样的, 使用friend function
[CODE]
#include <iostream>
using namespace std;
class Point
{
private:
int x;
int y;
public:
Point (int a = 0,int b = 0)
{
x=a;
y=b;
}
void set(int a,int b);
int get_x(){return x;}
int get_y(){return y;}
void print() const
{
cout<<"("<<x<<","<<y<<")\n";
}
friend Point operator + (const Point & a, const Point & b);
};

void Point::set(int a,int b)
{
x=a;
y=b;
}

Point operator + (const Point & a, const Point & b)
{
return Point(a.x + b.x, a.y + b.y);
}

int main()
{
Point a,b;
a.set(3,2);
b.set(1,5);

(a+b).print();
(a+b).print();
return 0;
}

[/CODE]

你的程序也可以是这样的, 使用member function
[CODE]
#include <iostream>
using namespace std;
class Point
{
private:
int x;
int y;
public:
Point (int a = 0,int b = 0)
{
x=a;
y=b;
}
void set(int a,int b);
int get_x(){return x;}
int get_y(){return y;}
void print() const
{
cout<<"("<<x<<","<<y<<")\n";
}
Point & operator + (const Point &a);
};

void Point::set(int a,int b)
{
x=a;
y=b;
}

Point & Point::operator + (const Point & a)
{
this->x += a.x;
this->y += a.y;
return *this;
}

int main()
{
Point a,b;
a.set(3,2);
b.set(1,5);

(a+b).print();
(a+b).print();
return 0;
}

[/CODE]

你可以比较一下这两者间的区别

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2006-12-09 12:13
一二三四五
Rank: 3Rank: 3
等 级:新手上路
威 望:8
帖 子:856
专家分:0
注 册:2006-11-13
收藏
得分:0 

学习了


hey,di va la
2006-12-09 12:57
maoguoqing
Rank: 6Rank: 6
来 自:重庆
等 级:贵宾
威 望:28
帖 子:2980
专家分:19
注 册:2005-12-5
收藏
得分:0 

好像第一个程序还是会有那样的编译错误。。。


天行健,君子以自强不息!!QQ:68660681
2006-12-10 00:45
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
maoguoqing,

好像是很不严谨的一种说法。

如果程序在你的机器上运行时出错, 那么请你给出出错信息, 这样我们可以继续讨论下去。 同时也希望你能提供你所使用的编译器的名称。

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2006-12-10 01:13
zhubenben
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2006-3-28
收藏
得分:0 
Point & Point::operator + (const Point & a)
{
this->x += a.x;
this->y += a.y;
return *this;
}

这个地方好像并不需要this->x
显示指出吧
Point & Point::operator + (const Point & a)
{
x += a.x;
y += a.y;
return *this;
}
这样就可以
类中每个成员函数都有个this指针
当对象调用这个函数时
this则指向那个对象
所以完全没必要写出
2006-12-10 22:02
maoguoqing
Rank: 6Rank: 6
来 自:重庆
等 级:贵宾
威 望:28
帖 子:2980
专家分:19
注 册:2005-12-5
收藏
得分:0 
以下是引用kai在2006-12-10 1:13:40的发言:
maoguoqing,

好像是很不严谨的一种说法。

如果程序在你的机器上运行时出错, 那么请你给出出错信息, 这样我们可以继续讨论下去。 同时也希望你能提供你所使用的编译器的名称。

不好意思,我说好像其实是因为我也没有运行这个程序。。
我是用VC++6.0, 错误信息:
tal error C1001: INTERNAL COMPILER ERROR
(compiler file 'msc1.cpp', line 1786)
Please choose the Technical Support command on the Visual C++
Help menu, or open the Technical Support help file for more information
Error executing cl.exe.
改为#include <iostream.h>这样引用头文件后便正确
但是我确实不明白是为什么,VS2003上很正确。。。

[此贴子已经被作者于2006-12-19 13:24:25编辑过]


天行健,君子以自强不息!!QQ:68660681
2006-12-19 13:23
快速回复:[求助]友元 重载+出错
数据加载中...
 
   



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

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