镭鐳sleepy丶 2019-04-13 23:43
浏览 165

这段程序的主要问题在哪里啊?为什么vs2017调试要很久而且最后一句输出不了a?

#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;

class String {
    char *str;
    unsigned int length;
public:
    String() {
        str = NULL;
        length = 0;
    }
    String(const char *s){
        str = NULL;
        length = strlen(s);
        if (s) {
            str = new char[strlen(s) + 1];
            strcpy(str, s);
        }
    }
    String(const String&other) {
        str = NULL;
        length = strlen(other.str);
        if (other.str) {
            str = new char[strlen(other.str) + 1];
            strcpy(str, other.str);
        }
    }
    ~String() {
        delete[](str);
        length = 0;
    }
    String& append(const String& other) {
        str = strcat(str, other.str);
        return *this;
    }
    String& operator = (const String &other) {
        delete[](str);
        str = NULL;
        length = other.length;
        if (other.str) {
            str = new char[strlen(other.str) + 1];
            strcpy(str, other.str);
        }
        return *this;
    }
    String& operator += (const String &other) {
        strcat(str, other.str);
        length = length + other.length;
        return *this;
    }
    String operator + (const String &other) {
        if (str == NULL)
            return other;
        if (other.str == NULL)
            return *this;
        String s;   
        s.length = other.length + length;
        s.str = new char[strlen(str) + strlen(other.str) + 1];
        strcpy(s.str, str);
        strcat(s.str, other.str);
        return s;
    }
    String& operator<< (const String &other) {
        if (str == NULL) {
            str = new char[strlen(other.str) + 1];
            length = other.length;
            strcpy(str, other.str);
            return *this;
        }
        if (other.str == NULL)
            return *this;
        strcat(str, other.str);
        length = length + other.length;
        return *this;
    }
    char& operator[](const int n) {
        return str[n];
    }
    friend ostream& operator<< (ostream&out, String& a);
};
    ostream& operator<< (ostream&out,String& a) {
        printf("%s\n", a.str);
        return out;
    }
int main() {
    String a("This is "), b(a);
    String c;
    cout << "a:" << a << "\n" << "b:" << b << "\n" << "c:" << c << "\n";
    c = "a test.";
    cout << "c:" << c << "\n";
    a.append(c);
    cout << "a:" << a << "\n";
    {
        String a("Hello"), b;
        a += " World";
        b << (a + b);
        cout << "b:" << b << "\n";
        String c;
        c = b + "!";
        cout << "c:" << c << "\n";
    }
    a[0] = 't';
    cout << "a:" << a << "\n";
    return 0;
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 运筹学排序问题中的在线排序
    • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
    • ¥30 求一段fortran代码用IVF编译运行的结果
    • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
    • ¥15 C++ 头文件/宏冲突问题解决
    • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
    • ¥50 安卓adb backup备份子用户应用数据失败
    • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
    • ¥30 python代码,帮调试,帮帮忙吧
    • ¥15 #MATLAB仿真#车辆换道路径规划