【问题标题】:Why it's not possible to assign std::string to a subtring?为什么不能将 std::string 分配给子字符串?
【发布时间】:2017-06-09 13:05:44
【问题描述】:

这是我要编译的项目的代码。错误在问题的末尾说明。

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

class substring {
    friend std::ostream& operator<<(std::ostream& output, substring const& sub);

public:
    char *str;
    int length;

    substring();
    ~substring();
    substring(std::string);
    substring(const substring &);

    substring& operator=(substring const& other);
    substring& operator=(std::string const& strz);

    substring& operator+=(substring const& other);

    bool operator>(substring const& other) const;
    bool operator<(substring const& other) const;

    char& operator[](size_t idx);
    char operator[](size_t idx) const;

};

std::ostream& operator<<(std::ostream& output, substring const& sub);
std::istream& operator >> (std::istream& input, substring const& sub);


bool operator==(substring const& one, substring const& another); 
bool operator!=(substring const& one, substring const& another); 

bool operator==(std::string const& str, substring const& sub); 
bool operator==(substring const& sub, std::string const& str);
bool operator!=(std::string const& str, substring const& sub);
bool operator!=(substring const& sub, std::string const& str); 


substring::substring()
{
    length = 0;
}

substring::~substring()
{
    delete str;
}

substring::substring(std::string)
{
}

substring::substring(const substring & sub)
{
    str = new char[length];
}

std::ostream & operator<<(std::ostream & output, substring const & sub)
{
    output << sub;
    return output;
}

std::istream & operator >> (std::istream & input, substring const & sub)
{
    std::cout << "Enter  sub:";
    input >> sub;
    return input;
}

bool operator==(substring const & one, substring const & another)
{
    if (one.length != another.length)
        return false;
    else
    {
        for (int i = 0; i < another.length; i++)
            if (one[i] != another[i])
            {
                return false;
                break;
            }
    }
    return true;
}

bool operator!=(substring const & one, substring const & another)
{
    if (one.length != another.length)
        return true;
    else
    {
        for (int i = 0; i < another.length; i++)
            if (one[i] != another[i])
            {
                return true;
                break;
            }
    }
    return false;
}

bool operator==(std::string const & str, substring const & sub)
{

    if (sub.length != str.length())
        return false;
    else
    {
        for (int i = 0; i < sub.length; i++)
            if (str[i] != sub[i])
            {
                return false;
                break;
            }
    }
    return true;
}

bool operator==(substring const & sub, std::string const & str)
{
    if (str.length() != sub.length)
        return false;
    else
    {
        for (unsigned int i = 0; i < str.length(); i++)
            if (sub[i] != str[i])
            {
                return false;
                break;
            }
    }
    return true;
}

bool operator!=(std::string const & str, substring const & sub)
{
    if (sub.length != str.length())
        return true;
    else
    {
        for (int i = 0; i < sub.length; i++)
            if (str[i] != sub[i])
            {
                return true;
                break;
            }
    }
    return false;
}

bool operator!=(substring const & sub, std::string const & str)
{
    if (sub.length != str.length())
        return true;
    else
    {
        for (int i = 0; i < sub.length; i++)
            if (str[i] != sub[i])
            {
                return true;
                break;
            }
    }
    return false;
}

substring & substring::operator=(substring const & other)
{
    delete str;
    length = other.length;
    str = new char[length];
    for (int i = 0; i<length; i++)
    {
        str[i] = other.str[i];
    }
    return *this;
}

substring & substring::operator=(std::string const & strz)
{
    length = strz.length();
    str = new char[length];
    for (int i = 0; i<length; i++)
    {
        str[i] = strz[i];
    }
    return *this;
}

substring & substring::operator+=(substring const & other)
{
    char* new_str = new char[length + other.length];
    for (int i = 0; i<length; i++)
    {
        new_str[i] = str[i];
    }
    for (int i = length; i<other.length; i++)
    {
        new_str[i] = other.str[i];
    }
    delete str;
    str = new_str;
    return *this;
}

bool substring::operator>(substring const & other) const
{
        return true;
}

bool substring::operator<(substring const & other) const
{
        return true;
}

char & substring::operator[](size_t idx)
{
    return str[idx];
}

char substring::operator[](size_t idx) const
{
    return str[idx];
}

int main()
{
    std::string str = "abc";
    substring sub = str;

    std::cout << sub;

    return 0;
}

问题是当我运行这段代码时,编译器似乎只是跳过了这个:substring sub = str;

我什至无法将此行更改为substring sub = "aaa";,因为它显示了一个错误,提示我无法将 subtring 转换为 std::string(尽管代码中有一个操作重载)。

【问题讨论】:

  • 好吧,你不告诉它做任何事情substring::substring(std::string){}
  • 我们能看到一些实现吗?
  • substring s = ... 不是赋值,而是初始化:它调用构造函数(你不应该使用 = 初始化值,而是使用构造函数)。
  • @NathanOliver 我应该在里面写什么?
  • @SaeedZeroOne 你需要初始化strlength

标签: c++ overloading stdstring


【解决方案1】:

问题是当我运行这段代码时,编译器似乎只是跳过了这个:substring sub = str;

编译器没有“跳过它”。编译成功,新对象创建成功。但是,您定义的转换构造函数使对象成员默认初始化:

substring::substring(std::string)
{
}

我什至无法将此行更改为substring sub = "aaa";,因为它显示了一个错误,提示我无法将子字符串转换为 std::string

我非常怀疑这一点。我怀疑你看错了。

我的编译器说const char [4] 不能转换substring

(尽管在代码中有一个操作重载)。

当然没有substring::substring(const char(&amp;)[4])(也没有substring::substring(const char*))。

【讨论】:

  • 对于第一个,我应该在括号内写什么?而对于答案的第二部分和第三部分,我应该如何将 const char 转换为字符串?
  • @SaeedZeroOne 我不知道。你的班级应该如何表现?您当然应该对str 做点什么,因为它将在析构函数中被删除,并且删除未初始化的指针具有未定义的行为。
  • @SaeedZeroOne 你的意思是 const char 还是 const char 数组?
  • 复制构造函数也不复制,但至少不会影响这段代码。
猜你喜欢
  • 1970-01-01
  • 2021-09-09
  • 1970-01-01
  • 2015-07-11
  • 1970-01-01
  • 2021-08-03
  • 2015-03-24
  • 2022-06-29
  • 2019-09-20
相关资源
最近更新 更多