【问题标题】:a value of type "const wchar_t *" cannot be used to initialize an entity of type "const PWSTR" [duplicate]“const wchar_t *”类型的值不能用于初始化“const PWSTR”类型的实体[重复]
【发布时间】:2019-12-22 19:27:18
【问题描述】:

我有一个 Visual Studio 2019 C++ 项目,该项目在一个头文件中有此定义:

static const PWSTR s_rgComboBoxStrings[] =
{
    L"First",
    L"Second",
    L"Third",
};

在这种情况下,会显示此错误:

a value of type "const wchar_t *" cannot be used to initialize an entity of type "const PWSTR"

这是为什么呢?如果我使用 PCWSTR 而不是 const PWSTR 它会编译,但问题是我在 Windows SDK .h 文件中遇到了同样的问题,所以修改 windows .h 文件不是一个好主意。

奇怪的是,我还有其他项目具有完全相同的定义(因为我只是复制并粘贴了代码),并且该项目完美编译。

我已经比较了项目设置,但似乎它们都是一样的。

我想过把所有的项目文件都复制过来,然后根据自己的项目需求重命名,改一下,但是我想先看看这个项目有什么问题。

问候 詹姆

【问题讨论】:

  • @AdrianMole 不...我知道区别。实际上,PCWSTR 定义为CONST WCHAR *PCWSTR,而PWSTR 定义为WCHAR *。因此,使用const PWSTR 时的定义是等价的。我的理论是某些项目设置正在影响。正如我所说,另一个具有相同代码的项目可以工作。
  • const WCHAR * != const PWSTR - 重读第一条评论中的链接
  • s_rgComboBoxStrings 在您的代码中是 const 数组到 WCHAR* 但不是 const WCHAR* 数组
  • const PWSTR 根本不等于 PCWSTR

标签: c++ windows visual-studio


【解决方案1】:

假设我们升起

typedef struct T * PT;

在这种情况下const PT != const T*const PT == T* const

在本例中可见

struct T 
{
    void operator++();
};
typedef struct T * PT;

void fn (const T* q)
{
    const PT p = q;// (1) Conversion loses qualifiers
    p++;           // (2) you cannot assign to a variable that is const
    *p = *q;       // (3) ok
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-23
    • 1970-01-01
    • 2019-08-25
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多