Container.cc: In member function ‘std::string Container::tostring()’: Container.cc:125:27: error: invalid operands of types ‘const char*’ and ‘const char [2]’ to binary ‘operator+’ cadena= "("+ this->id+ ","+this->weight +","+ this->price+")";
我怎么能解决这个错误,是c++
这是代码:
string Container::tostring()
{
cadena= "("+ this->id+ ","+this->weight +","+ this->price+")";
return cadena;
}
最佳答案
你至少应该这样写
string Container::tostring()
{
cadena= "("+ std::to_string( this->id ) + "," + std::to_string( this->weight ) + "," + std::to_string( this->price ) + ")";
return cadena;
}
另一种方式如下
#include <sstream>
//...
string Container::tostring()
{
std::ostringstream os;
os << "(" << this->id << "," << this->weight << "," << this->price << ")";
cadena = os.str();
return cadena;
}
关于c++ - 字符串连接,错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30571880/