遇到一个比较奇异的异常
遇到一个比较奇异的错误
error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const std::string' (or there is no acceptable conversion)
VS2008
奇异不在错误本身,而是我声明一个成员变量
这是什么原因造成的错误?如何会让string类型变化为const string?
------解决方案--------------------
你那个m_men所在的情况是不是像这样的?
error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const std::string' (or there is no acceptable conversion)
VS2008
奇异不在错误本身,而是我声明一个成员变量
//.h
std::string m_men;
//.cpp
std::string::size_type index = m_men.find_first_not_of(" ");
m_men = (std::string)m_men.substr(index); // C2678
//同
//std::string strTemp = (std::string)m_men.substr(index);
//m_men = strTemp; // C2678
这是什么原因造成的错误?如何会让string类型变化为const string?
------解决方案--------------------
你那个m_men所在的情况是不是像这样的?
void C::foo() const // 如果是这种情况,该方法中this会变为const C*类型的指针
{
/* 假设m_men是C的成员变量 */
m_men = strTemp; // 相当于static_cast<const C*>(this)->m_men = strTemp,自然出错
}