need help in string in MFC

I'm starting to learn MFC with visual studio 2005. I've got a little problem.
I posted this on CodeGuru but the answers are not satisfiable.
I'm also not using ATL. I specified "not using ATL" and "use MFC in a shared DLL" in the project properties.
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' 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
For a string literal like "abcde", is it default to CString
Thank you so much.


Answer this question

need help in string in MFC

  • Nataraj S. Narayana

    Thank you very much for your extremely fast and useful reply.
  • Chike

    I assume your project is by default created as a unicode project.

    Either:

    AfxMessageBox(L"abcde");
    or
    AfxMessageBox(_T("abcde"));

    will work.

    Same for the assignement:

    CString astring = L"abcde";
    or
    CString astring = _T("abcde");





  • Demonica

    Take a look at the tchar _T macros. They are extremly useful to program with and without unicode.

    http://msdn2.microsoft.com/en-us/library/c426s321.aspx



  • need help in string in MFC