首页 期权学习期权知识正文

Visual Studio error C2001:常量中有换行符的解决方法

xiaojiucai 期权知识 2020-08-18 435 0

    在Visual Studio自动生成的项目中,碰见了一件关于文件编码的问题,集中在类似于以下的语句上:

DASLog (DASProtWarn, L"(%s)消息超时,进入慢循环召唤模式。", GetHierarchyName());

  编译时会出现以下错误:

 error C2001:常量中有换行符

      该错误的原因很显然是文件编码的问题,在网上搜索了一下,找到了如下解决办法:

(1)全部用英文编码,不要用中文

(2)偶数中文 或 结尾加英文的符号,如"."

(3)将文件编码进行一个手动(如记事本)转换,改成UTF-8格式

      我采用了第二种方式,直接将末尾的中文“。”改成了英文的“.”,该错误就解决了!

至于该问题的详细原因,参考:https://www.qiquanji.com/utykn/1332.html 

有个叫wva的人遇到过类似问题,他向微软提交了此bug

http://connect.microsoft.com/VisualStudio/feedback/details/341454/compile-error-with-source-file-containing-utf8-strings-in-cjk-system-locale

根据Visual C++ Compiler Team员工的解释:

The compiler when faced with a source file that does not have a BOM the compiler reads ahead a certain distance into the file to see if it can detect any Unicode characters - it specifically looks for UTF-16 and UTF-16BE - if it doesn't find either then it assumes that it has MBCS. I suspect that in this case that in this case it falls back to MBCS and this is what is causing the problem.

看见了吧,对于那些没有BOM的文件设计就是这样的。从语气上看,他们编译器小组也不打算修改设计。所以呢,在VC上使用“无签名的UTF-8”编码的文件,你就是在抱着一颗不定时炸弹玩耍。因为你永远都不敢确定哪些词能通过编译,哪些不能!

 

如果要硬编码字符串,即便是字符编码转换也不一定能帮不上你。一旦你为此增加了字符编码转换的代码,那么也意味着可移植性降低了。因为这从根本上是编译器决定的。

原文链接:https://www.qiquanji.com/post/8429.html

本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。

微信扫码关注

更新实时通知

版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

评论