获取模糊符号的错误,需要帮助才能删除它

nov*_*ice 3 c++

我收到此错误,我无法在visual studio 2010中删除.我正在使用一个第三方库,它使用自己的"字符串"定义Visual Studio的xstring文件也在它安装的文件夹中.现在,当我试图编译代码时,我得到以下错误

1> ...\xyz.cpp(24):错误C2872:'string':模糊符号1>可以是'第三方库路径\ string.h(31)1>或'c:\ program files(x86)\microsoft visual studio 10.0\vc\include\xstring(2063):std :: string'

编译器无法理解它应该使用哪个字符串定义.如何在visual studi 2010中删除此错误.我希望代码使用第三方字符串定义.

我试图在包含目录中设置第三方路径,但我仍然看到此错误.请帮我.提前致谢

Boj*_*zec 10

这是命名空间冲突的示例.你可能在你的代码中:

#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?
Run Code Online (Sandbox Code Playgroud)

编译器现在不知道您要使用哪个字符串 - 一个来自第三方库或STL一个.您可以通过将名称空间名称添加到类型来解决此歧义:

3rdPartyNamespace::string myStr; // if you want to use string from 3rd party library
Run Code Online (Sandbox Code Playgroud)

要么

std::string myStr; // if you want to use STL string
Run Code Online (Sandbox Code Playgroud)

永远不要放在using namespace namespace_name;标题中,但也要尝试在源文件中避免它.最好的做法是预先添加类型名称,因为它不会污染您当前的命名空间,从而避免命名空间冲突.


小智 5

发明了命名空间以防止这些歧义。确保您从不使用using namespace stdusing namespace无论如何std::都是不正确的做法,并且键入“ ”的时间不会太长)并且应该没问题,只要std::string您想要标准字符串就可以使用。

// Without "using namespace std;"
string foo; // string from third party library used.
std::string bar; // string from standard library used.
Run Code Online (Sandbox Code Playgroud)