简体   繁体   English

错误 C2679:二进制“<<”:未找到采用“mystring”类型的右侧操作数的运算符(或没有可接受的转换)

[英]error C2679: binary '<<': no operator found which takes a right-hand operand of type 'mystring' (or there is no acceptable conversion)

So i tried re-implementing my own string class using smart pointers just so I can practice integrating them into my daily use.所以我尝试使用智能指针重新实现我自己的字符串类,这样我就可以练习将它们集成到我的日常使用中。 Sadly, I've hit a wall, and I can't figure out why I'm unable to concatenate two "mystring" objects.可悲的是,我撞到了一堵墙,我不明白为什么我无法连接两个“mystring”对象。 Any suggestions?有什么建议? Also, if this type of program isn't the proper situation to use smart pointers in, I would also appreciate advice related to that as well.另外,如果这种类型的程序不适合使用智能指针,我也很感激与此相关的建议。

#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;
}

Change the parameter message 's type of operator<< from mystring & (reference to non-const) to const mystring & (reference to const):将参数messageoperator<<类型从mystring & (对非常量的引用)更改为const mystring & (对 const 的引用):

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

operator+ returns by value, so what it returns is a temporary, which can't be bound to reference to non-const. operator+按值返回,所以它返回的是一个临时的,不能绑定到非常量的引用。

Note that you should do this not only for solving this issue;请注意,您应该这样做不仅是为了解决这个问题; message shouldn't be passed by reference to non-const because the argument is not supposed to be modified inside operator<< .不应通过对非常量的引用传递message因为不应在operator<<内部修改参数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C2679 二进制“-=”:未找到采用“T”类型右侧操作数的运算符(或没有可接受的转换) - C2679 binary '-=': no operator found which takes a right-hand operand of type 'T' (or there is no acceptable conversion) c ++错误c2679:二进制&#39;[&#39;:未找到采用&#39;SalesItem&#39;类型的右侧操作数的运算符(或者没有可接受的转换) - c++ error c2679: binary '[' : no operator found which takes a right-hand operand of type 'SalesItem' (or there is no acceptable conversion) 错误1错误C2679:二进制&#39;&lt;&lt;&#39;:未找到采用“合理”类型的右侧操作数的运算符(或没有可接受的转换) - Error 1 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'rational' (or there is no acceptable conversion) 错误2错误C2679:二进制&#39;/&#39;:找不到哪个操作符采用类型的右操作数(或者没有可接受的转换) - Error 2 error C2679: binary '/' : no operator found which takes a right-hand operand of type (or there is no acceptable conversion) 错误C2679:binary&#39;=&#39;:找不到运算符,它接受类型&#39;std :: vector &lt;_Ty&gt; *&#39;的右手操作数(或者没有可接受的转换) - error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::vector<_Ty> *' (or there is no acceptable conversion) 错误 C2679:二进制“&lt;&lt;”:未找到采用“RatNum”类型的右侧操作数的运算符(或没有可接受的转换) - error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'RatNum' (or there is no acceptable conversion) 错误C2679二进制&#39;=&#39;:找不到使用&#39;int&#39;类型的右侧操作数的运算符(或者没有可接受的转换) - Error C2679 binary '=': no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion) 错误:C2679二进制&#39;==&#39;:未找到采用类型为&#39;const std :: string&#39;的右侧操作数的运算符(或者没有可接受的转换 - Error:C2679 binary '==': no operator found which takes a right-hand operand of type 'const std::string' (or there is no acceptable conversion 错误C2679:二进制&#39;&gt;&gt;&#39;:未找到采用&#39;const char [4]&#39;类型的右侧操作数的运算符(或没有可接受的转换)22 - Error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'const char [4]' (or there is no acceptable conversion) 22 错误C2679:二进制&#39;&gt;&gt;&#39;:找不到哪个运算符采用&#39;std :: string&#39;类型的右手操作数(或者没有可接受的转换) - error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM