鱼C论坛

 找回密码
 立即注册
查看: 3134|回复: 3

重载“+”运算符,哪里出错了啊???

[复制链接]
发表于 2018-1-24 08:26:54 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include<iostream>
#include<string.h>
using namespace std;
#define maxsize 100
class str
{
private:
    char gro[maxsize];
    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;
}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-1-24 09:25:18 | 显示全部楼层
你的重载函数传入的是char*, 而你使用的却是str类的两个对象的加法
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-1-26 14:16:25 | 显示全部楼层
BngThea 发表于 2018-1-24 09:25
你的重载函数传入的是char*, 而你使用的却是str类的两个对象的加法

改了还是不对
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-1-26 18:01:05 | 显示全部楼层
只是能通过编译

  1. #include<iostream>
  2. #include<string.h>
  3. using namespace std;
  4. #define maxsize 100
  5. class str
  6. {
  7. private:
  8.         char gro[maxsize];
  9.         int length;
  10. public:
  11.         str();
  12.         str operator+(str tt);
  13.         void showing();
  14. };
  15. int main()
  16. {
  17.         str group1, group2, group;
  18.         group = group1 + group2;
  19.         group.showing();
  20.         return 0;
  21. }
  22. str::str()
  23. {
  24.         str s1;
  25.         cin >> s1.gro;
  26.         length = strlen(s1.gro);
  27.         strcpy(gro, s1.gro);
  28. }

  29. str str::operator+(str tt)
  30. {
  31.         /*cin >> s2;
  32.         strcpy(gro, s2.gro);
  33.         length = strlen(s2.gro);
  34.         str temp;
  35.         int length1;
  36.         length1 = strlen(gor) + strlen(s2.gro) + 1;
  37.         if(length1>maxsize)
  38.         {
  39.                 cout << "已超出最大限度" << endl;
  40.                 strcpy(temp.gro, s2);
  41.                 length = strlen(s2);
  42.                 cout << s2 << endl;
  43.         }
  44.         length = length1;
  45.         strcat(temp.gro, s2.gro);
  46.         return temp;*/

  47.         return str();
  48. }
  49. void str::showing()
  50. {
  51.         cout << gro << endl;
  52.         cout << "该字符串长度为:" << length << endl;
  53. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-5 23:42

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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