CString 怎么不能用+操作?

来源:百度知道 编辑:UC知道 时间:2024/05/17 02:28:51
代码:
CString ftpClass::ConnectFtp(const CString sUrl)
{
CString sResult;
CFtpConnection *Ftpconnection=NULL;
sResult+="Tring to connect Ftp sites"+sUrl+"\r\n";

Ftpconnection=m_session.GetFtpConnection(sUrl);
if(Ftpconnection)
{
sResult+="Connection established.\r\n";
CString sCurDir;
Ftpconnection->GetCurrentDirectory(sCurDir);
sResult+="current directory is"+sCurDir+"\r\n";
Ftpconnection->Close();
}
else
{
sResult+="There are some errors in finding this Ftp sites";
}
return sResult;
}

错误提示:
Error 1 error C2679: binary '+' : no operator found which takes a right-hand operand of type 'const char [3]' (or there is no acceptable conversion) c:\users\hao\documents\visual studio 2008\projects\ht

CString sResult;
CFtpConnection *Ftpconnection=NULL;
sResult+="Tring to connect Ftp sites"+sUrl+"\r\n";

原因是: sResult 没有初始化,你就拿来sResult = sResult+ "";

当然报错..

改成: CString sResult="";

函数参数const CString sUrl这样使用const好像没有任何意义,不如去掉算了。后面字符串连加的时候第一个字符串写成CString:sResult += CString("Tring to connect Ftp sites")+sUrl+"\r\n";。

----------------------
试了一下,如下代码,没有问题:
ATL::CAtlString strA;
const ATL::CAtlString strB = "a.b.c.d";
strA += ATL::CAtlString("1234") + strB + "\r\n";

ATL::CAtlString 就是 CString
编译环境是VS.NET

----
程序的问题不在CString 上,而是ftpclass类及其调用有问题
1)ftpclass在头文件里面添加成员函数的实现要么写在类的定义体里面,要写在外面就必须在函数实现前面增加inline。
2)ChttpDlg里面调用ftpclass的成员函数必须有一个对象实例才行,象这样
m_EditResult+=ftpClass.ConnectHttp(m_Url);是不对的,要现在ChttpDlg里面增加成员变量
ftpClass m_fc;
调用时