C++ 出现歧义符号错误,需要帮助将其删除
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9360375/
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
getting error for ambiguous symbol and need help to remove it
提问by novice
i am getting this error which i unable to remove in visual studio 2010. i am using one third party library which uses its own definition for "string" Also visual studio's xstring file is there in the folder where it gets installed. now when i am trying to compile code i am getting following error
我收到这个错误,我无法在 Visual Studio 2010 中删除它。我正在使用一个第三方库,它使用自己的“字符串”定义另外,visual studio 的 xstring 文件位于它安装的文件夹中。现在,当我尝试编译代码时,出现以下错误
1>...\xyz.cpp(24): error C2872: 'string' : ambiguous symbol 1> could be 'third party library path\string.h(31) 1> or 'c:\program files (x86)\microsoft visual studio 10.0\vc\include\xstring(2063) : std::string'
1>...\xyz.cpp(24): error C2872: 'string' : ambiguous symbol 1> 可能是 '第三方库路径\string.h(31) 1> 或 'c:\program files (x86) \Microsoft Visual Studio 10.0\vc\include\xstring(2063):std::string'
compiler is not able to understand which string definition it should use. How can i remove this error in visual studi 2010. I want the code to use third party string definition.
编译器无法理解它应该使用哪个字符串定义。如何在 Visual Studio 2010 中删除此错误。我希望代码使用第三方字符串定义。
i tried to set third party path in include directory but still i am seeing this error. Please help me. Thanks in advance
我试图在包含目录中设置第三方路径,但我仍然看到这个错误。请帮我。提前致谢
回答by Bojan Komazec
This is an example of a namespace clash. You probably have in your code:
这是命名空间冲突的一个示例。您的代码中可能有:
#include <3rdPartyString.h> // declaration of 3rd party string type
#include <string> // declaration of std::string
using namespace 3rdPartyNamespace;
using namespace std;
...
string myStr; // which string type?
Compiler now doesn't know which string you want to use - one from 3rd party library or STL one. You can resolve this ambiguity by prepending namespace name to the type:
编译器现在不知道您要使用哪个字符串 - 一个来自 3rd 方库或 STL 一个。您可以通过将命名空间名称添加到类型来解决这种歧义:
3rdPartyNamespace::string myStr; // if you want to use string from 3rd party library
or
或者
std::string myStr; // if you want to use STL string
Never place using namespace namespace_name;
in headers but try to avoid it in source files as well. The best practice is to prepend type name as this does doesn't pollute your current namespace with the other one thus avoiding namespace clashes.
永远不要放在using namespace namespace_name;
头文件中,但也要尽量避免在源文件中使用它。最佳做法是在类型名称前面加上类型名称,因为这不会污染您当前的命名空间,从而避免命名空间冲突。
回答by Bojan Komazec
Namespaces were invented to prevent these ambiguities. Make sure you never use using namespace std
(that specific using namespace
is bad practice anyway, and "std::
" isn't that long to type) and it should be fine, just use std::string
if you want the standard string.
命名空间的发明是为了防止这些歧义。确保你永远不会使用using namespace std
(using namespace
无论如何,那个特定的做法是不好的做法,并且“ std::
”输入时间不长)并且它应该没问题,std::string
如果你想要标准字符串就使用。
// Without "using namespace std;"
string foo; // string from third party library used.
std::string bar; // string from standard library used.
回答by Firedragon
The two definitions of string are clashing with each other so the compiler doesn't know what to use so you need a way to differentiate the two and that's where namespaces come in.
string 的两个定义相互冲突,因此编译器不知道该使用什么,因此您需要一种方法来区分两者,这就是命名空间的用武之地。
You can use the namespace that your third party string uses when referring to that string as the errors you are showing implies you have using namespace std
in your code.
您可以使用第三方字符串在引用该字符串时使用的命名空间,因为您显示的错误暗示您using namespace std
在代码中存在。
回答by rahvin_t
I ran into this issue recently by adding a Class Library project to a very large solution that included an in-house String library, the using namespace System;
in the pre-generated header was causing the ambiguity.
我最近通过将一个类库项目添加到一个非常大的解决方案中遇到了这个问题,该解决方案包含一个内部字符串库,using namespace System;
预生成的标头中的 导致了歧义。
回答by nikk
There is System.String. If not intended to use, make sure to indicate otherwise. Example System::String, or Custom::String
有 System.String。如果不打算使用,请确保另有说明。示例 System::String 或 Custom::String