鱼C论坛

 找回密码
 立即注册
查看: 2786|回复: 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;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-1-24 09:25:18 | 显示全部楼层
你的重载函数传入的是char*, 而你使用的却是str类的两个对象的加法
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

改了还是不对
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-1-26 18:01:05 | 显示全部楼层
只是能通过编译
#include<iostream>
#include<string.h>
using namespace std;
#define maxsize 100
class str
{
private:
        char gro[maxsize];
        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;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-1 09:47

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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