错误C2664:'CComboBox :: InsertString':不能将参数2从'const char [4]'转换为'LPCTSTR' [英] error C2664: 'CComboBox::InsertString' : cannot convert parameter 2 from 'const char [4]' to 'LPCTSTR'
问题描述
我试图做以下事情:
class sig
{
CComboBox objList;
void SetDefault();
}
void sig :: SetDefault()
{
objList.InsertString(0,METHOD_ONE);
}
我已将METHOD_ONE定义在不同的类中
#define METHOD_ONEOFF
<
>
Chintan
最重要的部分是了解错误;知道什么是 const char [4]
,是很容易的部分,但是 LPCTSTR
? >
根据 Microsoft文档:
一个
LPCWSTR
如果定义了UNICODE,则LPCSTR
。更多
信息,请参阅字符串的Windows数据类型。
而 LPCWSTR
是:
指向16位Unicode字符的常量空终止字符串的指针。有关详细信息,请参阅字体使用的字符集。
首先,必须检查程序使用的编码类型;你似乎使用 UNICODE
,所以最后你试图将const指针转换为 char
s(OFF常量)指向 wchar_t
的常量指针,并且不允许转换(逻辑)。
然后,可以选择正确的字符串类型;如果定义了UNICODE,您的 #define
必须是宽字符串:
//注意L
#define METHOD_ONE LOFF
这样:
#ifdef UNICODE
#define METHOD_ONE LOFF
#else
#define METHOD_ONEOFF
#endif
或使用 _T
宏建议的宏。这个宏只是将 L
前缀添加到文本:
#ifdef UNICODE
#define _T(x)L ## x
#else
#define _T x
#endif
最后,你必须知道什么样的字符串使用;但是Microsoft通过使用 #define
s和 typedef
的隐藏链来隐藏它。
I am trying to do the following:
class sig
{
CComboBox objList;
void SetDefault();
}
void sig :: SetDefault()
{
objList.InsertString(0, METHOD_ONE);
}
I have defined METHOD_ONE in a different class as
#define METHOD_ONE "OFF"
And I get the above error.
Can somebody please help me?
Cheers,
Chintan
The most important part is to understand the error; know what is a const char [4]
, is the easy part but, what about the LPCTSTR
?
According to the Microsoft documentation:
An
LPCWSTR
if UNICODE is defined, anLPCSTR
otherwise. For more information, see Windows Data Types for Strings.
And the LPCWSTR
is:
A pointer to a constant null-terminated string of 16-bit Unicode characters. For more information, see Character Sets Used By Fonts.
First, you must check out what type of encoding are using your program; it seems that you're using UNICODE
, so in the end you're trying to convert a const pointer to char
s (the "OFF" constant) to a const pointer to wchar_t
, and (logically) the conversion isn't allowed.
Then, you can choose the correct string type; if UNICODE is defined, your #define
must be wide string:
// Note the L
#define METHOD_ONE L"OFF"
You can also define it this way:
#ifdef UNICODE
#define METHOD_ONE L"OFF"
#else
#define METHOD_ONE "OFF"
#endif
Or use the _T
macro suggested by Roman R. The only that this macro does is to append L
prefix to the text:
#ifdef UNICODE
#define _T(x) L ##x
#else
#define _T(x) x
#endif
In the end, you must be aware of what kind of string are using; but Microsoft is hidding it by using an obscure chain of #define
s and typedef
s.
这篇关于错误C2664:'CComboBox :: InsertString':不能将参数2从'const char [4]'转换为'LPCTSTR'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!