我收到此错误,我无法在visual studio 2010中删除。我正在使用一个第三方库,它使用自己的“字符串”定义Visual Studio的xstring文件也在它安装的文件夹中。现在,当我尝试编译代码时,我收到了以下错误
1> ... \ xyz.cpp(24):错误C2872:'string':模糊符号 1 GT;可能是'第三方库路径\ string.h(31) 1 GT;或'c:\ program files(x86)\ microsoft visual studio 10.0 \ vc \ include \ xstring(2063):std :: string'
编译器无法理解它应该使用哪个字符串定义。如何在visual studi 2010中删除此错误。我希望代码使用第三方字符串定义。
我试图在include目录中设置第三方路径,但我仍然看到此错误。 请帮我。提前致谢
答案 0 :(得分:9)
这是命名空间冲突的示例。您可能在代码中有:
#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?
编译器现在不知道您要使用哪个字符串 - 一个来自第三方库或STL一个。您可以通过将名称空间名称添加到类型:
来解决此歧义3rdPartyNamespace::string myStr; // if you want to use string from 3rd party library
或
std::string myStr; // if you want to use STL string
永远不要将using namespace namespace_name;
放在标题中,但也要尝试在源文件中避免它。最好的做法是预先添加类型名称,因为它不会污染您当前的命名空间,从而避免命名空间冲突。
答案 1 :(得分:4)
发明命名空间是为了防止这些含糊不清。确保你永远不会使用using namespace std
(特定的using namespace
是不好的做法,并且“std::
”输入的时间并不长,而且应该没问题,只需使用{{1如果你想要标准的字符串。
std::string
答案 2 :(得分:1)
字符串的两个定义相互冲突,因此编译器不知道要使用什么,因此您需要一种方法来区分这两者以及名称空间的来源。
您可以使用第三方字符串在引用该字符串时使用的命名空间,因为您显示的错误意味着您的代码中包含using namespace std
。
答案 3 :(得分:0)
我最近遇到了这个问题,将类库项目添加到包含内部字符串库的非常大的解决方案中,预生成的标头中的using namespace System;
导致了歧义。
答案 4 :(得分:0)
有System.String。如果不打算使用,请务必另行说明。示例System :: String或Custom :: String