重载“+”运算符,哪里出错了啊???
#include<iostream>#include<string.h>
using namespace std;
#define maxsize 100
class str
{
private:
char gro;
int length;
public:
str();
str operator+(char*s2);
void showing();
};
int main()
{
str group1,group2,group;
group=group1+group2;
group.showing();
return 0;
}
str::str()
{
str s1;
cin>>s1.gro;
length=strlen(s1.gro);
strcpy(gro,s1.gro);
}
str str::operator+(char *s2)
{
cin>>s2;
strcpy(gro,s2.gro);
length=strlen(s2.gro);
str temp;
int length1;
length1=strlen(gor)+strlen(s2.gro)+1;
if(length1>maxsize)
{
cout<<"已超出最大限度"<<endl;
strcpy(temp.gro,s2);
length=strlen(s2);
cout<<s2<<endl;
}
length=length1;
strcat(temp.gro,s2.gro);
return temp;
}
void str::showing()
{
cout<<gro<<endl;
cout<<"该字符串长度为:"<<length<<endl;
}
{:10_243:} {:10_243:} 你的重载函数传入的是char*, 而你使用的却是str类的两个对象的加法 BngThea 发表于 2018-1-24 09:25
你的重载函数传入的是char*, 而你使用的却是str类的两个对象的加法
改了还是不对{:10_266:} 只是能通过编译
#include<iostream>
#include<string.h>
using namespace std;
#define maxsize 100
class str
{
private:
char gro;
int length;
public:
str();
str operator+(str tt);
void showing();
};
int main()
{
str group1, group2, group;
group = group1 + group2;
group.showing();
return 0;
}
str::str()
{
str s1;
cin >> s1.gro;
length = strlen(s1.gro);
strcpy(gro, s1.gro);
}
str str::operator+(str tt)
{
/*cin >> s2;
strcpy(gro, s2.gro);
length = strlen(s2.gro);
str temp;
int length1;
length1 = strlen(gor) + strlen(s2.gro) + 1;
if(length1>maxsize)
{
cout << "已超出最大限度" << endl;
strcpy(temp.gro, s2);
length = strlen(s2);
cout << s2 << endl;
}
length = length1;
strcat(temp.gro, s2.gro);
return temp;*/
return str();
}
void str::showing()
{
cout << gro << endl;
cout << "该字符串长度为:" << length << endl;
}
页:
[1]