售前咨询
技术支持
渠道合作

Visual Studio error C2001:常量中有换行符(解决办法)

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

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

编译时会出现以下错误:

 error C2001:常量中有换行符

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

 

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

 

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

 

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

 

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

 

至于该问题的详细原因,参考一位网友的分析说明(引用:http://www.cnblogs.com/cocos2d-x/archive/2012/02/26/2368873.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”编码的文件,你就是在抱着一颗不定时炸弹玩耍。因为你永远都不敢确定哪些词能通过编译,哪些不能!

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

上一篇:

下一篇:

相关文章