IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> cannot convert argument 4 from ‘const wchar_t [5]‘ to ‘LPOLESTR‘ -> 正文阅读

[C++知识库]cannot convert argument 4 from ‘const wchar_t [5]‘ to ‘LPOLESTR‘

00现象

上代码

void function(LPOLESTR ptName)
{
......
}
void main()
{
	function(L"name");
}

环境win10, 编译器VS2017
function(L"the name");这一行,出现提示

argument of type "const wchar_t*" is incompatible with parameter of type "LPOLESTR"

强行编译,这一行的错误信息是:

cannot convert argument 1 from 'const wchar_t [5]' to 'LPOLESTR'

01背景

这段代码是从一个实际工程里面抽离出来的,在原工程中没有问题,可是拷贝到新的工程上面就不行了。
由于有上面的背景,所以考虑从两个方面来调查这个问题,一个是比较两个工程配置有什么不同,另一个是直接去查找原因。
打开两个工程的配置,把原工程的配置都复制到新的上面,仍然不行。于是放弃第一条路,开始第二条路的探索,就是直接查找原因。

02问题探索

LPOSTR

查找LPOLESTR,发现是一个typedef,并且搜索找到了这样一段话。

LPOLESTR is a string of OLECHAR which is essentially wchar_t. So LPOLESTR is a null-terminated wchar_t*. LPOLESTR is a typedef created by Microsoft. These are vestiges of an automatic ANSI / Unicode conversion scheme that Microsoft used prior to MFC 4.0 and has since abandoned. For Win32 development, “OLE” corresponds to Unicode. For example, in Win32 development, an OLECHAR is simply a wchar_t and an LPOLESTR is a wide character string (e.g. wchar_t*).
(引自:https://stackoverflow.com/questions/6682508/how-to-convert-wstring-to-lpolestr)
也想到了一个方案:把代码改成

void function(LPOLESTR ptName)
{
......
}
void main()
{
	wchar_t names[20] = L"name";
	function(names);
}

编译通过了。

const

可是还是有个疑问,LPOLESTR 应该就是wchar_t*,而根据提示L"the name"的类型为const wchar_t [5],那么是不是const的原因呢?
尝试一下把function定义改为

void function(const LPOLESTR ptName)

这次鼠标放上去不再提示错误了,可是编译的时候仍然不过,错误提示变成了
cannot convert argument 4 from ‘const wchar_t [8]’ to ‘const LPOLESTR’

没招了,探索暂时到这吧。

03再次出现

机缘巧合,重新编译了原工程,大跌眼镜的是原工程编译不过了,报了一样的错误,怎么回事,我都干了些什么?
还好有git,查一下原工程都修改了什么。
赫然发现在vcxproj有改动,多了一行

<ConformanceMode>true</ConformanceMode>

这就奇怪了,难道是因为之前复制旧工程配置的时候复制反了,把新工程的复制到旧工程了?
把这行删掉,再试,果然编译通过了。
再把新工程里的ConformanceMode改成No,也能编译通过了。
看来就是这个原因了。

04回头看

既然找到了解决方法,那就试着再次去找找原因吧。

conformanceMode

搜索这个ConformanceMode是干啥用的,找到了这篇:
const char _ 类型的实参与 char _ 类型的形参不兼容错误的解决方法
还有这样一段
The /permissive- option uses the conformance support in the current compiler version to determine which language constructs are non-conforming. The option doesn’t determine if your code conforms to a specific version of the C++ standard. To enable all implemented compiler support for the latest draft standard, use the /std:c++latest option. To restrict the compiler support to the currently implemented C++20 standard, use the /std:c++20 option. To restrict the compiler support to the currently implemented C++17 standard, use the /std:c++17 option. To restrict the compiler support to more closely match the C++14 standard, use the /std:c++14 option, which is the default.
引自 :https://docs.microsoft.com/en-us/cpp/build/reference/permissive-standards-conformance?view=msvc-150

看来问题还是const不兼容的原因,那么为什么跟ConformanceMode有关系呢?

回到const

又找到这段话
In C, string literals are of type char[], and can be assigned directly to a (non-const) char*. C++03 allowed it as well (but deprecated it, as literals are const in C++). C++11 no longer allows such assignments without a cast.

引自:https://stackoverflow.com/questions/55751920/default-argument-of-type-const-char-is-incompatible-with-parameter-of-type
还有个解释string literals的
https://en.cppreference.com/w/cpp/language/string_literal
从这个上看,C和C++03支持直接使用非const类型,但是C++11不支持了,必须要强制转换。所以看来是C++标准的问题。
而ConformanceMode就是用于设置符合标准的情况的,所以设置为No的话不检查是否符合新标准。就能编译通过了。

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-05-12 16:18:51  更:2022-05-12 16:19:15 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/10 11:01:04-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码