C ++无法从枚举转换为LPCTSTR

时间:2014-06-05 11:17:14

标签: c++ visual-studio

我在Visual Studio中编译游戏源代码,并在编译时发现了一些错误。

  

错误C2440:'初始化' :无法转换   ' eKind'到LPCTSTR'   从整数类型到指针类型的转换需要reinterpret_cast,C风格的转换或函数式转换

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
};

问题是什么?怎么解决这个问题?

2 个答案:

答案 0 :(得分:1)

enum是标量数据类型。 LPCTSTR是指向charwchar_t的指针的typedef(取决于Unicode设置)。

C ++不允许从标量到指针的隐式转换。使用适当的源数据类型将其分配给LPCTSTR

在您的示例中,您希望定义一个TCHAR数组,而不是指向它的指针。

TCHAR thai[255] = {
    NONE,   NONE,  
    // ...
};

答案 1 :(得分:1)

例如,与C#不同,您可以在C / C ++中获取枚举的字符串表示形式。如果你想要这样的事情,你需要自己写一些。例如:

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

  return "unknown";
}