1

I'm new to MFC and I don't know what to do with this error.

ERROR

error C2664: 'void ATL::CStringT::Format(const wchar_t *,...)' : cannot convert parameter 1 from 'const char [6]' to 'const wchar_t *'

heres the line:

m_Echo1.Format("%d %",state.dwMemoryLoad);
user2668338
  • 13
  • 1
  • 4

2 Answers2

2

By default a Windows app is set to use 16-bit characters, not 8-bit characters. Change your quoted string to L"%d %" to specify a string of 16-bit characters.

ScottMcP-MVP
  • 10,337
  • 2
  • 15
  • 15
  • 2
    Two comments: It's almost always better to use the T macros - so `_T("%d %%")`. And a single, standalone `%` is not a valid format specifier. I believe that newer versions of the Microsoft libraries raise a security exception at runtime when they encounter one. – Nik Bougalis Aug 10 '13 at 15:26
2

There are 2 distinct errors with the line of code you posted:

  1. The format string contains an illegal format specifier (trailing %). If you want a format string to contain a literal percent-sign it has to be escaped using %%.
  2. You are using a string literal that does not match the required encoding, i.e. a mismatch between ANSI and UNICODE character encoding. If m_Echo1 is of type CString the parameter has to be wrapped inside a _T or TEXT macro: _T( "%d %%" ). If m_Echo1 is of type CStringW the parameter must be passed as a UNICODE string literal by prepending it with L: L"%d %%".

Note: The error message you posted does not match the line of code. The error message refers to const char [6] while the string literal in your code is of type const char [5]. Make sure that error messages and code match up.

IInspectable
  • 46,945
  • 8
  • 85
  • 181