Problem with CString in visual C++ 2005 (MFC)

from:http://community.sgdotnet.org/forums/p/9044/26197.aspx

 

Let's look at the definition of AfxMessageBox.

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

This is what we're looking at. LPCTSTR is a LPCWSTR if unicode, or LPCSTR if not. LPCSTR actually stands for "Long Pointer Constant String" if you understand your hungarian notation.

So let's see what's different from here?

First of all, to fix that, you'll just have to do something like this.

void CHelloDialogDlg::OnBnClickedOk()
{
// TODO: Add your control notification handler code here
AfxMessageBox(_T("abcde")); // or L"abcde"
OnOK();
}

The difference is that the current "abcde" you declared is not defined as a long pointer, but a "short" pointer (if I may say that). In order to declare it as a long pointer, you're required to add an additional "L", or use the macro _T(x) or _TEXT(x). I'm not going to go into details what long pointers are, I'm sure you can google it. L is just to convert the string of characters into wide-character literal which LPCSTR (str is actually talking about wchar_t).

Ultimately, it's all very confusing and I won't even want to delve into the messy explanations. But that's that. I might be wrong though, because it's been almost 6 or 7 years since I last coded in vanilla MFC/C++ windows programming. Forgive me. :P

 

galaxy_nus:

 

I'm starting to learn MFC with visual studio 2005. I've got a little problem.
In a dialog button OK, I added an event handler like this:

void CHelloDialogDlg::OnBnClickedOk()
{
// TODO: Add your control notification handler code here
AfxMessageBox("abcde");
OnOK();
}

it won't compile. The error is:

error C2665: 'AfxMessageBox' : none of the 2 overloads could convert all the argument types

When I change to this:

void CHelloDialogDlg::OnBnClickedOk()
{
// TODO: Add your control notification handler code here
CString astring = "abcde";
AfxMessageBox(astring);
OnOK();
}

The error is:
error C2440: 'initializing' : cannot convert from 'const char Devil [6]' to 'ATL::CStringT<BaseType,StringTraits>'

It works well when i write this way:

void CHelloDialogDlg::OnBnClickedOk()
{
// TODO: Add your control notification handler code here
CString astring;
astring = "abcde";

AfxMessageBox(astring);
OnOK();
}


Could anyone show me what's wrong with my code?
Thank you so much.
 
//
aladdin: So pls. pay attention to this problem while the current project is using "Unicode" character set.
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值