64,637
社区成员
发帖
与我相关
我的任务
分享
#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 ambiguousVC6.0....... 运行也是OK的,你用什么编译器测试的??
友元放类里面跟外面运行上有差别吗? 可以了!这是为什么啊? 友元函数定义函数放在类内试试 VC6.0....... 运行也是OK的,你用什么编译器测试的??
可以了!这是为什么啊? 友元函数定义函数放在类内试试 VC6.0....... 运行也是OK的,你用什么编译器测试的??
友元函数定义函数放在类内试试 VC6.0....... 运行也是OK的,你用什么编译器测试的??
运行也是OK的,你用什么编译器测试的??