错误 C2678:二进制'=':未找到采用 'const std::string' 类型左侧操作数的运算符(或没有可接受的转换)

error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const std::string' (or there is no acceptable conversion)

本文关键字:运算符 操作数 可接受 转换 std 二进制 C2678 string 错误 const 类型      更新时间:2023-10-16

我真的很困惑,为什么我得到以下编译错误。Microsoft Visual Studio Compiler.

error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const std::string' (or there is no acceptable conversion)

#include <stdio.h>
#include <iostream>
#include <sstream>
#include <iterator>
class MyException {
public:
    MyException(    std::string message, 
                        int line = 0) : m_message(message),
                                        m_line(line) {}
    const char* what() const throw(){
        if ( m_line != 0 ) {
            std::ostringstream custom_message;
            custom_message << "Parsing Error occured at ";
            custom_message << m_line << " Line : ";
            custom_message << m_message;        
            m_message = custom_message.str();
        }
        return m_message.c_str();
    }
private:
    std::string m_message;
    int m_line;
};
int main(int argc, char **argv) {
    try {
        // do something
    }catch(MyException &e){
        std::cout << e.what();
    }
}

在行中出现错误m_message = custom_message.str();

方法声明为const

const char* what() const throw(){

但是你试图改变对象

m_message = custom_message.str();

得到一个错误。

你应该做的是在构造函数中构造自定义消息。

class MyException {
public:
    MyException(const std::string& message, int line = 0) : 
        m_message(message), m_line(line) {
        if ( m_line != 0 ) {
            std::ostringstream custom_message;
            custom_message << "Parsing Error occured at ";
            custom_message << m_line << " Line : ";
            custom_message << m_message;        
            m_message = custom_message.str();
        }
    }
    const char* what() const throw(){
        return m_message.c_str();
    }
private:
    std::string m_message;
    int m_line;
};

我也改变了你的代码通过引用传递std::字符串,这是通常的做法。

您正在尝试在const限定方法MyException::what()中分配MyException::m_message。在这样的what()中,整个*this对象被认为是const,这意味着m_message成员也是const。不能将任何值赋给const限定的std::string对象,因为std::string的赋值操作符需要在左侧有一个可修改的(即非const对象)对象。您提供的是const

如果你真的想修改what()中的m_message,你应该将它声明为类的mutable成员(在这种情况下,这似乎是一个好主意)。或者使用其他方法。

正如@john所指出的,在您的特定情况下,在构造函数中实际构建m_message而不是将其推迟到what()更有意义。我真的不明白为什么你每次调用what()都要重建m_message。除非您的m_line期望以某种方式从对what()的调用更改为另一个,否则真的没有必要每次都这样做。

除了其他答案;

您没有包含<string>标头,这可能是以后出现问题的原因。

曾经让我很多的东西是一些std::头包括其他人,这允许你使用一个类,但可能只有有限的功能,因为它们包含的std::头是最小的,这是该文件运行所需的。这是相当恼人的,因为有时你声明一个std::类,如string,你还没有包括头,定义将很好,但其他一切可能或可能不工作-导致你大量的调试,因为定义工作良好。

参见what()函数的声明,它被标记为const(该行上的第二个const)。这意味着它不能改变任何成员变量,在您的例子中是m_message字符串。这就是为什么你会得到错误。

现在,你如何解决它?

你的代码是错误的,你的what()函数将前置"Parsing Error occured at "等文本每次你调用what()函数。因此,与其修改m_message成员,我建议您在类的元素中格式化整个消息:

MyException(std::string message, int line = 0)
{
    if (line != 0)
    {
        std::ostringstream custom_message;
        custom_message << "Parsing Error occured at ";
        custom_message << line << " Line : ";
        custom_message << message;        
        m_message = custom_message.str();            
    }
    else
        m_message = message;
}
相关文章: