找回密码
 注册
搜索
热搜: 回贴
  • 前程无忧官网首页 有什么好的平台可以
  • 最新的销售平台 互联网营销的平台有哪
  • 制作网页的基本流程 网页制作和网页设
  • 【帝国CMS】输出带序号的列表(数字排
  • 网站建设公司 三一,中联,极东泵车的
  • 织梦 建站 织梦网站模版后台怎么更改
  • 云服务官网 哪些网站有免费的简历模板
  • 如何建网站要什么条件 建网站要用什么
  • 吉林市移动公司电话 吉林省退休人员网
  • 设计类毕业论文 网站设计与实现毕业论
查看: 1659|回复: 5

关于加号重载

[复制链接]
发表于 2009-11-3 01:43:17 | 显示全部楼层 |阅读模式 IP:江苏扬州
#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)
这是什么错误
发表于 2009-11-3 01:43:19 | 显示全部楼层 IP:江苏扬州
没像你这样重载的,错了不少呢
看看这个
#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;
}
回复

使用道具 举报

发表于 2009-11-3 01:43:21 | 显示全部楼层 IP:江苏扬州
我要的是类的每一个数据成员加上一个整数!不是两个类的相加!
你的程序我刚才运行了一下 还是有错误:
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)
回复

使用道具 举报

发表于 2009-11-3 01:43:24 | 显示全部楼层 IP:江苏扬州
我也觉得我的vc6.0有问题.好象一用到友元就有哪个错.
不知道有没有哪个高手指点一下.
回复

使用道具 举报

发表于 2009-11-3 01:43:26 | 显示全部楼层 IP:江苏扬州
友元还要这个域解析运算符?
回复

使用道具 举报

发表于 2009-11-3 01:43:28 | 显示全部楼层 IP:江苏扬州
看看这个行不?我是用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");
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-9-30 07:23 , Processed in 0.229809 second(s), 13 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表