开发者

'<function-style-cast>' : cannot convert from 'int' to 'CString'

This code was written in Visual Studio 2003, but now I compile it in 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;
}

In 2003, it works fine, but in 2008, it shows me an error:

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

What does this error mean?


It's a bit hard to help without any information, like the erroneous code and what you want to do there.

Here's a guess: You want to convert an int to a CString, somehow like this:

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

If you're using the MFC/ATL CString you could try the following

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

CString::Format takes a format string like printf and stores the result in the CString.

Edit

I'm interpreting your comment below as the code that causes the error. A bit surrounding text and explanation would be nice, though.

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

Try to change it to

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

One general comment on the goto PreReturnCleanup;: You may want to take a look at the RAII-Idiom as a (imho) better way to do such cleanup.


This is obviously because you used an expression of type int where an expression of type CString was expected. No more can be said without further code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜