C++ cannot convert from enum to LPCTSTR [closed]

2019-09-19 23:37发布

I compile game source code in Visual Studio and found some error while compile.

error C2440: 'initializing' : cannot convert from 'eKind' to 'LPCTSTR' Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast

enum eKind
{
        NONE,
        CONSO,  //consonant
        V_UP,   //vowel in upper
        SV_UP,  //special vowel in upper
        V_SIDE, //vowel in side
        V_UN,   //vowel in under
        V_UPSI, //vowel in upper and side
        SOU,    //special in upper
        ENG,    //english and number
}
LastKind = NONE;

LPCTSTR thai[255] = {
        NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,
        NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,
        NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,
        ENG,    ENG,    ENG,    ENG,    ENG,    ENG,    ENG,    ENG,    ENG,    ENG,    NONE,   NONE,   NONE,   NONE,   NONE,   NONE,
        NONE,   ENG,    ENG,    ENG,    ENG,    ENG,    ENG,    ENG,    ENG,    ENG,    ENG,    ENG,    ENG,    ENG,    ENG,    ENG,
        ENG,    ENG,    ENG,    ENG,    ENG,    ENG,    ENG,    ENG,    ENG,    ENG,    ENG,    NONE,   NONE,   NONE,   NONE,   NONE,
        NONE,   ENG,    ENG,    ENG,    ENG,    ENG,    ENG,    ENG,    ENG,    ENG,    ENG,    ENG,    ENG,    ENG,    ENG,    ENG,
        ENG,    ENG,    ENG,    ENG,    ENG,    ENG,    ENG,    ENG,    ENG,    ENG,    ENG,    NONE,   NONE,   NONE,   NONE,   NONE,
        NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,
        NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,

        NONE,   CONSO,  CONSO,  CONSO,  CONSO,  CONSO,  CONSO,  CONSO,  CONSO,  CONSO,  CONSO,  CONSO,  CONSO,  CONSO,  CONSO,  CONSO,
        CONSO,  CONSO,  CONSO,  CONSO,  CONSO,  CONSO,  CONSO,  CONSO,  CONSO,  CONSO,  CONSO,  CONSO,  CONSO,  CONSO,  CONSO,  CONSO,
        CONSO,  CONSO,  CONSO,  CONSO,  CONSO,  CONSO,  CONSO,  CONSO,  CONSO,  CONSO,  CONSO,  CONSO,  CONSO,  CONSO,  CONSO,  CONSO,

        V_SIDE, V_UP,   V_SIDE, V_UPSI, V_UP,   V_UP,   V_UP,   V_UP,   V_UN,   V_UN,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,
        V_SIDE, V_SIDE, V_SIDE, V_SIDE, V_SIDE, V_SIDE, V_SIDE, SV_UP,  SOU,    SOU,    SOU,    SOU,    SOU,    NONE,   NONE,   NONE,
        NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE,   NONE
};

What's the problem? How to solve this?

2条回答
Rolldiameter
2楼-- · 2019-09-20 00:14

Unlike C#, for example, you can get a string representation of an enum in C/C++. If you want to so this sort of thing you'll need to write the some yourself. For example:

std::string eKindToString(eKind value)
{
  switch(value)
  {
    case foo: return "foo";
    case bar: return "bar";
    // etc
  }

  return "unknown";
}
查看更多
Juvenile、少年°
3楼-- · 2019-09-20 00:28

An enum is a scalar data type. LPCTSTR is a typedef of a pointer to char or wchar_t (depends on Unicode setting).

C++ does not allow the implicit conversion from a scalar to a pointer. Use an appropriate source data type to assign it to a LPCTSTR.

In your example you want to define an array of TCHAR instead of a pointer to it.

TCHAR thai[255] = {
    NONE,   NONE,  
    // ...
};
查看更多
登录 后发表回答