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

#include<iostream>
using namespace std;
class Myclass
{
int num;
public:
Myclass(int a){num=a;};
friend Myclass &operator +(const int a,const Myclass t);
};
Myclass & Myclass::operator +(const int a,const Myclass t)
{
this->num=a+t.num;
return *this;
}

错误提示是:
Compiling...
grre.cpp
E:\C++\c++\fgh\grre.cpp(8) : 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
Error executing cl.exe.

grre.obj - 1 error(s), 0 warning(s)
这是什么错误

搜索更多相关主题的帖子: 加号 重载 
2006-05-04 23:26
linlin
Rank: 1
等 级:新手上路
帖 子:134
专家分:0
注 册:2006-3-14
收藏
得分:0 

没像你这样重载的,错了不少呢
看看这个
#include<iostream>
using namespace std;
class Myclass
{
int num;
public:
Myclass(int a){num=a;};
void print() { cout<<"num = "<<num<<endl; }
friend Myclass operator +(const Myclass a,const Myclass t);
};
Myclass operator +(const Myclass a,const Myclass t)
{

return Myclass(a.num + t.num);
}
int main()
{
Myclass m1(1),m2(2);
Myclass m3 = m1 + m2;
m3.print();
system("pause");
return 0;
}


woyaochengshuyidianle 我真的什么也不会
2006-05-04 23:49
freshman42
Rank: 1
等 级:新手上路
威 望:1
帖 子:94
专家分:0
注 册:2005-12-4
收藏
得分:0 

我要的是类的每一个数据成员加上一个整数!不是两个类的相加!
你的程序我刚才运行了一下 还是有错误:
Compiling...
fjytjyt.cpp
e:\c++\c++\egdstgrer\fjytjyt.cpp(9) : 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
Error executing cl.exe.

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


2006-05-05 00:06
linlin
Rank: 1
等 级:新手上路
帖 子:134
专家分:0
注 册:2006-3-14
收藏
得分:0 

那个在我这边是可以的,怀疑你用的是VC6.0。
这个你用,在VC6。0可以用

#include<iostream>
using namespace std;
class Myclass
{
int num;
public:
Myclass(int a){num=a;};
void print() { cout<<"num = "<<num<<endl; }
Myclass & operator+(const int t);
};
Myclass & Myclass:: operator +( int t)//数据成员加上一个整数
{
this->num = this->num + t;
return *this;
}
int main()
{
Myclass m1(1);
m1 + 5;//
m1.print();
system("pause");
return 0;
}
如果,还不行的话,就让其他人回答了


woyaochengshuyidianle 我真的什么也不会
2006-05-05 01:00
freshman42
Rank: 1
等 级:新手上路
威 望:1
帖 子:94
专家分:0
注 册:2005-12-4
收藏
得分:0 

我也觉得我的vc6.0有问题.好象一用到友元就有哪个错.
不知道有没有哪个高手指点一下.


2006-05-05 01:20
wfpb
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:2188
专家分:0
注 册:2006-4-2
收藏
得分:0 
友元还要这个域解析运算符?

[glow=255,red,2]wfpb的部落格[/glow] 学习成为生活的重要组成部分!
2006-05-05 16:40
heliujin
Rank: 2
等 级:论坛游民
帖 子:249
专家分:14
注 册:2006-3-14
收藏
得分:0 

看看这个行不?我是用microsoft vc++6.0的环境下写的 运行出来你 看看值得不值得参考吧
#include<iostream>
using namespace std;
class myclass
{
int num;
public:
myclass(int a){num=a;}
myclass operator+(int);
void print();
};
myclass myclass::operator+(int t)
{
this->num=this->num+t;
return *this;
}
void myclass::print()
{
cout<<"num is :"<<num<<endl;
}
void main()
{
myclass a(6);
myclass b=a+6;
b.print();
system("pause");

}

2006-05-06 12:03
快速回复:关于加号重载
数据加载中...
 
   



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

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