今天在写程序编译的时候出现了如下的错误:
error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class CString' (or there is no acceptable conversion)
代码如下
void CBus::SetValue(CString strid,CString strruntime,CString stroriginstation,CString strterminalstation,CString strtotaltime,CString strmaxpeople,CString strnowpeople)
{
strId = strid;
strRunTime = strruntime;
strOriginStation = stroriginstation;
strTerminalStation = strterminalstation;
strTotalTime = strtotaltime;
strMaxPeople = strmaxpeople;
strNowPeople = strnowpeople;
}
其中strid,strmaxpeople,strnowpeople对应数据库里面的列名,数据类型为int,在执行编译的时候就出现了三处上面提示的错误,分别是大括号里第一行和最后两行,几经周折终于发现了问题所在,原来是将整形的strid赋值给了字符型的strId,其他两个同样如此。
解决办法,用itoa()函数将整数转换为字符串(Convert an integer to a string)
strId = atoi(strid); //将整形转换成字符串
strMaxPeople = atoi(strmaxpeople); //将整形转换成字符串
strNowPeople = atoi(strnowpeople); //将整形转换成字符串
再次编译就通过了
本文解决了C++中常见的编译错误C2679,该错误通常发生在尝试将不同类型的变量赋值给另一个类型不匹配的变量时。文中详细介绍了错误发生的原因,并提供了一个具体的例子来说明如何使用itoa函数将整数转换为字符串以解决此问题。

2426

被折叠的 条评论
为什么被折叠?



