visual-studio C++ 编译器错误 C2371 - WCHAR 的重新定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 16:26:17  来源:igfitidea点击:

C++ Compiler Error C2371 - Redefinition of WCHAR

c++visual-studio

提问by Brian Stewart

I am getting C++ Compiler error C2371 when I include a header file that itself includes odbcss.h. My project is set to MBCS.

当我包含一个本身包含 odbcss.h 的头文件时,我收到 C++ 编译器错误 C2371。我的项目设置为 MBCS。

C:\Program Files\Microsoft SDKs\Windows\v6.0A\include\odbcss.h(430) : error C2371: 'WCHAR' : redefinition; different basic types 1>
C:\Program Files\Microsoft SDKs\Windows\v6.0A\include\winnt.h(289) : see declaration of 'WCHAR'

C:\Program Files\Microsoft SDKs\Windows\v6.0A\include\odbcss.h(430):错误 C2371:'WCHAR':重新定义;不同的基本类型 1>
C:\Program Files\Microsoft SDKs\Windows\v6.0A\include\winnt.h(289) :参见'WCHAR'的声明

I don't see any defines in odbcss.h that I could set to avoid this. Has anyone else seen this?

我在 odbcss.h 中没有看到任何可以设置来避免这种情况的定义。有没有其他人看过这个?

回答by ChrisN

This is a known bug - see the Microsoft Connect website:

这是一个已知错误 - 请参阅 Microsoft Connect 网站:

http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=98699

http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=98699

The error doesn't occur if you compile your app as Unicode instead of MBCS.

如果您将应用程序编译为 Unicode 而不是 MBCS,则不会发生该错误。

回答by Brian Stewart

There are a half-dozen posts on various forums around the web about this - it seems to potentially be an issue when odbcss.h is used in the presence of MFC. Most of the answers involve changing the order of included headers (voodoo debugging). The header that includes odbcss.h compiles fine in it's native project, but when it is included in a different project, it gives this error. We even put it in the latter project's stdafx.h, right after the base include for MFC, and still no joy. We finally worked around it by moving it into a cpp file in the original project, which does not use MFC (which should have been done anyway - but it wasn't our code). So we've got a work-around, but no real solution.

在网络上的各种论坛上有六篇关于此的帖子 - 在存在 MFC 的情况下使用 odbcss.h 时,这似乎是一个潜在的问题。大多数答案都涉及更改包含标题的顺序(巫毒调试)。包含 odbcss.h 的头文件在它的本机项目中编译得很好,但是当它包含在不同的项目中时,它会出现这个错误。我们甚至把它放在后一个项目的 stdafx.h 中,就在 MFC 的基础包含之后,仍然没有任何乐趣。我们最终解决了这个问题,将它移动到原始项目中的一个 cpp 文件中,该文件不使用 MFC(无论如何应该这样做 - 但它不是我们的代码)。所以我们有一个变通方法,但没有真正的解决方案。

回答by Craig H

This error happens when you redeclare a variable of the same name as a variable that has already been declared. Have you looked to see if odbcss.h has declared a variable you already have?

当您重新声明与已声明的变量同名的变量时,会发生此错误。您是否查看了 odbcss.h 是否声明了您已经拥有的变量?

回答by John Boker

does this help?

这有帮助吗?

http://bytes.com/forum/thread602063.html

http://bytes.com/forum/thread602063.html

Content from the thread:

来自线程的内容:

Bruno van Dooren [MVP VC++] but i know the solution of this problem. it solves by changing project setting of "Treat wchar_t as Built-in Type" value "No (/Zc:wchar_t-)". But I am using "Xtreme Toolkit Professional Edition" for making good look & Feel of an application, when i fix the above problem by changing project settings a new linking errors come from Xtreme Toolkit Library. So what i do to fix this problem, in project setting "Treat wchar_t as Built-in Type" value "yes" and i wrote following statements where i included wab.h header file. You can change that setting on a per-codefile basis so that only specific files are compiled with that particular setting. If you can solve your problems that way it would be the cleanest solution.

#define WIN16

#include "wab.h"

#undef WIN16

and after that my project is working fine and all the things related to WAB is also working fine. any one guide me, is that the right way to solve this problem??? and, will this have any effect on the rest of project?? I wouldn't worry about it. whatever the definition, it is a 16 bit variable in both cases. I agree that it isn't the best looking solution, but it should work IF WIN16 has no other impact inside the wab.h file.

--

Kind regards, Bruno van Dooren [email protected] Remove only "_nos_pam"

Bruno van Dooren [MVP VC++] 但我知道这个问题的解决方案。它通过更改“将 wchar_t 视为内置类型”值“否 (/Zc:wchar_t-)”的项目设置来解决。但是我正在使用“Xtreme Toolkit Professional Edition”来使应用程序具有良好的外观和感觉,当我通过更改项目设置来解决上述问题时,新的链接错误来自 Xtreme Toolkit 库。那么我如何解决这个问题,在项目设置“将 wchar_t 视为内置类型”值“是”中,我写了以下语句,其中包含 wab.h 头文件。您可以在每个代码文件的基础上更改该设置,以便仅使用该特定设置编译特定文件。如果你能以这种方式解决你的问题,那将是最干净的解决方案。

#define WIN16

#include "wab.h"

#undef WIN16

在那之后,我的项目运行良好,所有与 WAB 相关的事情也运行良好。任何人指导我,这是解决这个问题的正确方法吗???而且,这会对项目的其余部分产生任何影响吗??我不会担心的。无论定义如何,在这两种情况下它都是一个 16 位变量。我同意它不是最好看的解决方案,但如果 WIN16 在 wab.h 文件中没有其他影响,它应该可以工作。

——

亲切的问候, Bruno van Dooren [email protected] 仅删除“_nos_pam”