【问题标题】:error C2679: binary '<<': no operator found which takes a right-hand operand of type 'mystring' (or there is no acceptable conversion)错误 C2679:二进制“<<”:未找到采用“mystring”类型右侧操作数的运算符(或没有可接受的转换)
【发布时间】:2018-02-11 08:12:10
【问题描述】:

所以我尝试使用智能指针重新实现我自己的字符串类,这样我就可以练习将它们集成到我的日常使用中。可悲的是,我碰壁了,我无法弄清楚为什么我无法连接两个“mystring”对象。有什么建议?此外,如果这种类型的程序不适合在其中使用智能指针,我也将感谢与此相关的建议。

#include <iostream>
#include <memory>
#include <cstring>

using namespace std;

class mystring {
public:
    mystring() : word(make_unique<char[]>('\0')), len(0) {}
    ~mystring() { cout << "goodbye objects!";}
    mystring(const char *message) : word(make_unique<char[]>(strlen(message) + 1)), len(strlen(message)) {
        for (int i = 0; i < len; i++) word[i] = message[i];
    }
    mystring(const mystring &rhs) : word(make_unique<char[]>(rhs.len)), len(rhs.len + 1) {
        for (int i = 0; i < len; i++) word[i] = rhs.word[i];
    }
    mystring &operator=(mystring &rhs) {
        if (this != &rhs) {
            char *temp = word.release();
            delete[] temp;
            word = make_unique<char[]>(rhs.len + 1);
            len = rhs.len;
            for (int i = 0; i < len; i++) word[i] = rhs.word[i];
        }
        return *this;
    }
    mystring &operator=(const char *rhs) {
        char *temp = word.release();
        delete[] temp;
        word = make_unique<char[]>(strlen(rhs)+ 1);
        len = strlen(rhs);
        for (int i = 0; i < len; i++) word[i] = rhs[i];

        return *this;
    }
    friend mystring operator+(const mystring& lhs, const mystring& rhs) {
        mystring Result;
        int lhsLength = lhs.len, rhsLength = rhs.len;

        Result.word = make_unique<char[]>(lhsLength + rhsLength + 1);
        Result.len = lhsLength + rhsLength;
        for (int i = 0; i < lhsLength; i++) Result.word[i] = lhs.word[i];
        for (int i = lhsLength; i < lhsLength + rhsLength; i++) Result.word[i] = rhs.word[i];

        return Result;
    }
    friend ostream &operator<<(ostream &os, mystring &message) {
        for (int i = 0; i < message.len; i++) os << message.word[i];
        return os;
    }
private:
    int len;
    unique_ptr<char[]> word;
};

int main()
{
    mystring word1 = "Darien", word2 = "Miller", word3;

    cout << word1 + word2;//error message: no binary '<' found
    word3 = word1 + word2;//error message: no binary '=' found
    cout << word3;
    return 0;
}

【问题讨论】:

  • 可能应该在所有建立缓冲区的地方将最终字符设置为'\0'
  • 其实这是个好主意。我会马上做的

标签: c++ visual-c++ smart-pointers


【解决方案1】:

将参数messageoperator&lt;&lt;类型从mystring &amp;(引用非const)更改为const mystring &amp;(引用const):

friend ostream &operator<<(ostream &os, const mystring &message) {
    for (int i = 0; i < message.len; i++) os << message.word[i];
    return os;
}

operator+按值返回,所以它返回的是一个临时的,不能绑定到non-const的引用。

请注意,您应该这样做不仅是为了解决这个问题; message 不应通过引用传递给非 const,因为不应在 operator&lt;&lt; 内修改参数。

【讨论】:

  • 哦,哇,好的,因此将 const 添加到 operator
  • @DarienMiller 因为非常量引用不能绑定到临时引用。谷歌它。
  • @DarienMiller 将非常量引用绑定到临时是被禁止的并且没有意义。例如你不能写int&amp; ri = 42 + 42;这样的东西。
  • @songyuanyao 复制。这很有意义。因此,通过将参数更改为 const,我无法执行以下操作:mystring word = "darien", word2 = "miller", word3; cout
  • @DarienMiller 我不知道;我建议你调试operator+operator=,你可能会发现一些意想不到的行为;或者您可以通过mcve 提出新问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多