c++ - 运算符重载问题

标签 c++ class operator-overloading

对于我的 CS 课,我们的老师希望我们创建自己的字符串课。我正处于开始阶段,目前正在尝试重载 operator+。他希望它作为非成员函数。这是我目前所拥有的:

这是 String 头文件:

#include <iostream>
#include <stdio.h>
#include <cstring>

#ifndef STRING_HPP
#define STRING_HPP



class String
{
private:
    int len;
    char* str;
public:
    String();               // default constructor
    String(String const& s);    // copy constructor
    String(char const* s);      // C-string constructor
    ~String() {delete str;};                // destructor
    char* const getString();    //get string for printing
    String& operator=(char const* c);
    String& operator=(String const& s);

};

inline std::ostream& operator<<(std::ostream& os, String s)
{
    return os << s.getString();
}

String operator+(String const& lhs, String const& rhs) {
    len = lhs.len + rhs.len;
    str = new char[len];
    std::strcat(str,lhs);
    std::strcat(str,rhs);
}

#endif

这是 String.cpp 文件:

#include "string.hpp"
#include <cstring>
#include <iostream>

String::String()
{
    len = 0;
    str = new char[len];
}

String::String(String const& s) // copy constructor
{
    len = s.len;
    str = new char[len]; 
    std::strcpy(str,s.str);

}
String::String(char const* s)   // C-string constructor
{
    len = std::strlen(s);
    str = new char[len];
    std::strcpy(str,s);

}

char* const String::getString()
{
    return str;
}

String& String::operator=(char const* c)
{
    // 1: allocate new memory and copy the elements
    int newLen = std::strlen(c);
    char* newStr = new char[newLen]; 
    std::strcpy(newStr,c);

    // 2: deallocate old memory
    delete [] str;

    // 3: assign the new memory to the object
    str = newStr;
    len = newLen;
}

String& String::operator=(String const& s)
{
    // 1: allocate new memory and copy the elements
    int newLen = s.len;
    char* newStr = new char[newLen]; 
    std::strcpy(newStr,s.str);

    // 2: deallocate old memory
    delete [] str;

    // 3: assign the new memory to the object
    str = newStr;
    len = newLen;
 }

这里是main的实现:

#include "string.hpp"
#include <iostream>
int main()
{

    String s1;
    String s2 = "test";
    String s3 = s2;

    std::cout << s1 << '\n';
    std::cout << s2 << '\n';
    std::cout << s3 << '\n';

    String a = "one";
    String b = "two";

    a = b;
    b = "three";

    std::cout << a << '\n';
    std::cout << b << '\n';

    String hello = "hello ";
    String world = "world";
    String concat = hello + world;

    std::cout << concat << '\n';
}  

一切正常,直到最后一个 concat。我无法更改 Main.cpp,因为它是由教授提供的。

谁能帮我指明正确的方向,让 operator+ 正常工作?

最佳答案

您正在使用

str = new char[len]; 

在所有功能中。那没有足够的内存来保存终止的空字符。您需要使用:

str = new char[len+1];

operaor+ 函数中,在内存分配之后使用 strcat 会导致未定义的行为,因为 str 未初始化。

你需要使用:

str = new char[len+1];
str[0] = '\0';
std::strcat(str,lhs);
std::strcat(str,rhs);

str = new char[len+1];
std::strpy(str,lhs);    // strcpy, not strcat
std::strcat(str,rhs);

更新

operator+()的完整实现:​​

String operator+(String const& lhs, String const& rhs) {
    int len = lhs.len + rhs.len;
    char* str = new char[len+1];
    std::strcpy(str,lhs);
    std::strcat(str,rhs);
    String ret(str);
    delete [] str;
    return ret;
}

如果您实现String::operator+=(String const& rhs),您可以简化函数。然后,您可以使用:

String operator+(String const& lhs, String const& rhs) {
    String ret(lhs);
    ret += rhs;
    return ret;
}

关于c++ - 运算符重载问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33269238/

相关文章:

c++ - 如何使语言友好功能降低?

c++ - 拷贝构造函数和赋值运算符的区别

c++ - 前向声明模板类时无效使用不完整类型

c++ - 重载 >> 时未获得预期值。我在这里做错了什么?

c++ - 使用 Visual Studio C++ 按名称搜索目录中的文件

c++ - 尝试在路由器上执行命令。 C++和libssh

vba - 类模块中的单击事件未触发

python - 如何引用没有定义变量的现有类对象?

c++ - 没有匹配的调用函数

使用数组类型时 Swift 3 `+` 运算符错误