为什么不能将std :: string分配给子字符串?

时间:2017-06-09 13:05:45

标签: c++ overloading stdstring

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

#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";,因为它显示错误,表示我无法将子字符串转换为std :: string(尽管代码中存在操作重载)。

1 个答案:

答案 0 :(得分:0)

  

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

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

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

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

我非常怀疑。我怀疑你误读了。

我的编译器说const char [4]无法转换为substring

  

(虽然在代码中有一个操作重载)。

肯定没有substring::substring(const char(&)[4])(也不是substring::substring(const char*))。