运算符重载时遇到的问题'operator +' is ambiguous

阿斯达所多 2016-06-10 10:09:43
#include<iostream>
#include<string>
using namespace std;

class Str
{
int length;
char *p;
public:
Str()
{
p=new char[100];
}
Str(char *s)
{
p=new char [100];
strcpy(p,s);
}
Str(const Str &obj)
{
p=new char [100];
strcpy(p,obj.getp());
}
~Str()
{
delete []p;
}
friend Str operator + (const Str &s1,const Str &s2);
void operator =(const Str &obj);
char* getp()const
{
return p;
}
void setp(char *s)
{
strcpy(p,s);
}
void show()
{
length=strlen(p);
cout<<"length="<<length<<" "<<p<<endl;
}
};

Str operator+(const Str &s1,const Str &s2)
{
Str temp;
strcpy(temp.getp(),s1.getp());
strcat(temp.getp(),s2.getp());
return temp;
}

void Str::operator =(const Str &obj)
{
strcpy(p,obj.getp());
}

int main()
{
char s1[100],s2[100];
cin.getline(s1,100);
cin.getline(s2,100);
Str A(s1);
Str B(s2);
Str C;
C=A+B;
C.show();
return 0;
}
运行时会编译错误,具体错误是error C2593: 'operator +' is ambiguous
这问题该如何解决
...全文
278 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
renwotao2009 2016-06-11
  • 打赏
  • 举报
回复
引用 3楼我是你的主体 的回复:
引用 2 楼 renwotao2009 的回复:
运行也是OK的,你用什么编译器测试的??
VC6.0.......
友元函数定义函数放在类内试试
paschen 版主 2016-06-11
  • 打赏
  • 举报
回复
引用 3 楼 Pointer_Success 的回复:
引用 2 楼 renwotao2009 的回复:
运行也是OK的,你用什么编译器测试的??
VC6.0.......
VC6对很多标准的支持都不好
小灸舞 2016-06-11
  • 打赏
  • 举报
回复
建议你换一个IDE,VC6太老了,换成VS
地址:http://blog.csdn.net/cometnet/article/details/19551125
如果觉得VS太大就下个轻量级的IDE,比如CODEBLOCKS
renwotao2009 2016-06-11
  • 打赏
  • 举报
回复
引用 7 楼 Pointer_Success 的回复:
引用 6 楼 Pointer_Success 的回复:
引用 4 楼 renwotao2009 的回复:
引用 3楼我是你的主体 的回复:
引用 2 楼 renwotao2009 的回复:
运行也是OK的,你用什么编译器测试的??
VC6.0.......
友元函数定义函数放在类内试试
可以了!这是为什么啊?
友元放类里面跟外面运行上有差别吗?
估计是编译器版本的问题,不用纠结这个
阿斯达所多 2016-06-11
  • 打赏
  • 举报
回复
引用 6 楼 Pointer_Success 的回复:
引用 4 楼 renwotao2009 的回复:
引用 3楼我是你的主体 的回复:
引用 2 楼 renwotao2009 的回复:
运行也是OK的,你用什么编译器测试的??
VC6.0.......
友元函数定义函数放在类内试试
可以了!这是为什么啊?
友元放类里面跟外面运行上有差别吗?
阿斯达所多 2016-06-11
  • 打赏
  • 举报
回复
引用 4 楼 renwotao2009 的回复:
引用 3楼我是你的主体 的回复:
引用 2 楼 renwotao2009 的回复:
运行也是OK的,你用什么编译器测试的??
VC6.0.......
友元函数定义函数放在类内试试
可以了!这是为什么啊?
lm_whales 2016-06-11
  • 打赏
  • 举报
回复
char* getp()const
阿斯达所多 2016-06-10
  • 打赏
  • 举报
回复
引用 2 楼 renwotao2009 的回复:
运行也是OK的,你用什么编译器测试的??
VC6.0.......
renwotao2009 2016-06-10
  • 打赏
  • 举报
回复
运行也是OK的,你用什么编译器测试的??
renwotao2009 2016-06-10
  • 打赏
  • 举报
回复
vs2012测试,没有编译错误啊!!!

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧