本文介绍了C++ 无法从枚举转换为 LPCTSTR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

错误 C2440:正在初始化":无法从'eKind' 到 'LPCTSTR'从整型到指针类型的转换需要reinterpret_cast、C-style cast或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?

推荐答案

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

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

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

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.

在您的示例中,您想要定义一个 TCHAR 数组而不是指向它的指针.

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

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

这篇关于C++ 无法从枚举转换为 LPCTSTR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 04:34