长不大的小Tom 2022-01-31 01:54 采纳率: 100%
浏览 24
已结题

c++输出操作符重载问题

//MyString.cpp
#include "MyString.h"

MyString::MyString()
{
    cout << "MyString()" << endl;
    this->len = 0;
    this->str = new char[this->len + 1];
    strcpy(this->str,"");
}

//MyString::MyString(int len)
//{
//    if (len <= 0)
//    {
//        this->len = 0;
//        this->str = new char[this->len + 1];
//        strcpy(this->str, "");
//    }
//    else
//    {
//        this->len = len;
//        this->str = new char[this->len + 1];
//        strcpy(this->str,str);
//    }
//}

MyString::MyString(const char* str)
{
    cout << "MyString(const char* str)" << endl;
    if (str == NULL)
    {
        this->len = 0;
        this->str = new char[this->len + 1];
        strcpy(this->str, "");
    }
    else
    {
        this->len = strlen(str);
        this->str = new char[this->len + 1];
        strcpy(this->str, str);
    }
}

MyString::MyString(const MyString& another)
{
    cout << "MyString(const MyString& another)" << endl;

    if (another.len <= 0)
    {
        this->len = 0;
        this->str = new char[this->len + 1];
        strcpy(this->str, "");
    }
    else
    {
        this->len = another.len;
        this->str = new char[this->len + 1];
        strcpy(this->str, another.str);
    }
}

MyString::~MyString()
{
    if (this->str != NULL)
    {
        cout << "~MyString()" << endl;
        delete[] this->str;
        this->str = NULL;
        this->len = 0;
    }
}

char& MyString::operator[](int index)
{
    return this->str[index];
}

MyString& MyString::operator=(const MyString& another)
{
    if (this == &another)
    {
        return *this;
    }

    if (this->str != NULL)
    {
        delete[] this->str;
        this->str = NULL;
        this->len = 0;
    }

    this->len = another.len;
    this->str = new char[this->len + 1];
    strcpy(this->str, another.str);
    cout << "MyString & MyString::operator=(MyString & another)" << endl;
    return *this;
}

#if 0
MyString& MyString::operator+(MyString& another)
{
    MyString tmp;
    if (this->str == NULL)
    {
        this->len = another.len;
        this->str = new char[this->len + 1];
        strcpy(this->str,another.str);
    }
    else
    {
        tmp.len = this->len;
        tmp.str = new char[tmp.len + 1];
        strcpy(tmp.str,this->str);

        delete[] this->str;
        this->str = NULL;
        this->len = 0;

        this->len = tmp.len + another.len;
        this->str = new char[this->len + 1];
        memset(this->str,0,this->len + 1);
        strcpy(this->str,tmp.str);
        strcat(this->str,another.str);
    }
    return *this;
}
#endif // 0

MyString MyString::operator+(MyString& another)
{
    MyString tmp;

    tmp.len = this->len + another.len;
    tmp.str = new char[tmp.len + 1];
    memset(tmp.str, 0, tmp.len + 1);
    strcpy(tmp.str, this->str);
    strcat(tmp.str, another.str);
    
    return tmp;
}

ostream& operator<<(ostream& os, MyString& s)
{
    cout << "ostream& operator<<(ostream& os, MyString& str)" << endl;
    os << s.str;
    return os;
}

istream& operator>>(istream& is, MyString& s)
{
    cout << "istream& operator>>(istream& is, MyString& str)" << endl;
    if (s.str != NULL)
    {
        delete[] s.str;
        s.str = NULL;
        s.len = 0;
    }

    char tmp_str[4096];
    is >> tmp_str;
    s.len = strlen(tmp_str);
    s.str = new char[s.len + 1];
    strcpy(s.str,tmp_str);

    return is;
}

//MyString.h
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>

using namespace std;

class MyString
{
public:
    MyString();
    //MyString(int len);
    MyString(const char* space);
    MyString(const MyString& another);
    ~MyString();

    //重载<<操作符
    friend ostream& operator<<(ostream& os, MyString& s);
    //重载>>操作符
    friend istream& operator>>(istream& is, MyString& s);
    //重载==操作符
    //重载!=操作符
    //重载+操作符
    MyString operator+(MyString& another);
    //重载=操作符
    MyString& operator=(const MyString& another);
    //重载[]操作符
    char& operator[](int index);
private:
    int len;
    char* str;
};

//main.cpp
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
#include "MyString.h"

using namespace std;

int main(void)
{
    MyString str1 = "abcd";
    cout << str1 << endl;

    //MyString str2 = "zxcv";
    //str1 = str2;
    //cout << str1 << endl;

    MyString str3 = "1234";
    //(str3 + str1) + str1;
    cout << (str3 + str1) + str1 << endl;
    //cout << str3 << endl;

    system("pause");
    return 0;
}

img

为什么提示我没有匹配。教学视频这样写的都没问题

  • 写回答

2条回答 默认 最新

  • SmallAntJ 2022-01-31 05:39
    关注

    因为 (str3 + str1) + str1的结果是一个常量,而<<的重载函数的参数没有用const关键字,所以无法输出一个MyString的常量,把重载函数改成 ostream& operator<<(ostream& os, const MyString& s) 就可以了。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 2月8日
  • 已采纳回答 1月31日
  • 创建了问题 1月31日

悬赏问题

  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 qgcomp混合物线性模型分析的代码出现错误:Model aliasing occurred
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答