gpt4 book ai didi

c++ - '' : cannot convert from ' int' to 'CString'

转载 作者:行者123 更新时间:2023-11-30 02:11:07 26 4
gpt4 key购买 nike

这段代码是在 Visual Studio 2003 中编写的,但现在我是在 2008 年编译的。

int AFXAPI AfxMessageBox(LPCTSTR lpszText, UINT nType = MB_OK, UINT nIDHelp = 0);

if(iiRecd == SOCKET_ERROR || iiRecd == 0) {
iErr = ::GetLastError();
AfxMessageBox(CString(iErr));
goto PreReturnCleanup;
}

在 2003 年,它工作正常,但在 2008 年,它向我显示错误:

Error 50 error C2440: '<function-style-cast>' : cannot convert from 'int' to 'CString'

这个错误是什么意思?

最佳答案

如果没有任何信息(例如错误代码和您想在那里做什么),很难提供帮助。

这是一个猜测:您想将 int 转换为 CString,就像这样:

int i = 42;
CString str = (CString)i;

如果您使用的是 MFC/ATL CString你可以试试下面的方法

int i = 42;
CString str;
str.Format("%d", i);

CString::Format采用类似 printf 的格式字符串并将结果存储在 CString 中。

编辑

我将您在下面的评论解释为导致错误的代码。不过,一些周围的文字和解释会很好。

if(iiRecd == SOCKET_ERROR || iiRecd == 0) { 
iErr = ::GetLastError();
AfxMessageBox(CString(iErr));
goto PreReturnCleanup;
}

试着改成

if(iiRecd == SOCKET_ERROR || iiRecd == 0) { 
iErr = ::GetLastError();
CString msg;
msg.Format("%d", iErr);
AfxMessageBox(msg);
goto PreReturnCleanup;
}

关于 goto PreReturnCleanup; 的一般评论:您可能想看一下 RAII -成语作为(恕我直言)进行此类清理的更好方法。

关于c++ - '<function-style-cast >' : cannot convert from ' int' to 'CString',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3890720/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com