今天在写程序编译的时候出现了如下的错误:
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); //将整形转换成字符串
再次编译就通过了