错误 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)
我真的很困惑,为什么我得到以下编译错误。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;
}
- 为什么C++逐位AND运算符在不同大小的操作数中表现为这样
- 错误 C2679:二进制"<<":未找到采用类型 'std::string_view' 的右侧操作数的运算符(或者没有可接受的转换)
- 是否所有C++运算符都接受操作数?
- 一元*运算符的操作数是否期望一个 prvalue
- C2678 二进制 '==':未找到采用 'Card' 类型左操作数的运算符(或者没有可接受的转换)
- 我的运算符重载是否有效<<(流插入)左操作数不是 ostream
- 没有运算符"="与这些操作数匹配,JUCE 窗口演示应用程序
- C2679 二进制"<<":未找到采用右操作数类型的运算符
- CPP 运算符重载操作数位置 [] 与 ==
- int* 和 int[] 类型对二进制运算符 + 的操作数无效
- 运算符'<'不能应用于类型 'string' 和 'string' 的操作数
- 错误 C2679 二进制"<<":未找到采用类型为 'T' 的右侧操作数的运算符
- 是否允许三元运算符在C++中计算两个操作数?
- 二进制 '==':未找到采用 'Enemy' 类型左侧操作数的运算符(或者没有可接受的转换)
- 与"运算符>>"不匹配(操作数类型为"QDataStream"和"QJsonObject")
- 类型"int"和"const char [15]"到二进制"运算符<<"的无效操作数^
- 如何摆脱逗号运算符的这个错误右操作数没有效果( wunsued 值)?
- c++ 错误:二进制"运算符+"类型"矩阵*"和"矩阵*"的操作数无效
- c++运算符重载-我实际返回的操作数类型是什么
- 没有运算符>>与这些操作数匹配 ifstream