[ACCEPTED]-getting error for ambiguous symbol and need help to remove it-c++
This is an example of a namespace clash. You probably have 10 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 9 string you want to use - one from 3rd party 8 library or STL one. You can resolve this 7 ambiguity by prepending namespace name to 6 the type:
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 5 try to avoid it in source files as well. The 4 best practice is to prepend type name as 3 this does doesn't pollute your current namespace 2 with the other one thus avoiding namespace 1 clashes.
Namespaces were invented to prevent these 4 ambiguities. Make sure you never use using namespace std
(that 3 specific using namespace
is bad practice anyway, and "std::
" isn't 2 that long to type) and it should be fine, just 1 use std::string
if you want the standard string.
// Without "using namespace std;"
string foo; // string from third party library used.
std::string bar; // string from standard library used.
The two definitions of string are clashing 7 with each other so the compiler doesn't 6 know what to use so you need a way to differentiate 5 the two and that's where namespaces come 4 in.
You can use the namespace that your third 3 party string uses when referring to that 2 string as the errors you are showing implies 1 you have using namespace std
in your code.
I ran into this issue recently by adding 4 a Class Library project to a very large 3 solution that included an in-house String 2 library, the using namespace System;
in the pre-generated header 1 was causing the ambiguity.
There is System.String. If not intended 2 to use, make sure to indicate otherwise. Example 1 System::String, or Custom::String
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.