Error Message
'symbol' : ambiguous symbolThe compiler cannot determine which symbol you are referring to.
C2872 can occur if a header file includes a using Directive (C++), and a subsequent header file is #include'd and contains a type that is also in the namespace specified in the using directive. Specify a using directive only after all your header files are specified with #include.
For more information about C2872, see http://support.microsoft.com/default.aspx?scid=kb;en-us;316317.
The following sample generates C2872:
// C2872.cpp
namespace A {
int i;
}
using namespace A;
int i;
int main() {
::i++; // ok
A::i++; // ok
i++; // C2872 ::i or A::i?
}
1) Don't put using namespace at global scope (i.e. not within nested { brackets } ) in any header file.
2) In your C++ files, put the #includes before the using namespaces, e.g.Unfortunately, that eliminates the convenience of putting lines like using namespace System; near the top of your header file, so that you don't have to qualify all of your CLR references. Sorry, but that's the breaks.
// MyHeader.h
#pragma once
using namespace System; // <-- Don't do this!
using namespace other::nice::stuff; // <-- Don't do this!The problem is that when a header has using namespace at the top of the file, it stays in effect after the header. It's considered bad practice, and in particular it hits a conflicting declaration of IServiceProvider.
Your alternatives are:
- Fully qualify your CLR references (so that every String now becomes System::String)
- Put the using namespace only inside of some scope, e.g.
ref class CNeatClass
{
using namespace System;
// Now I don't have to prefix System:: while I'm in my class declaration!
:
};
// Now that I'm outside of my class declaration, it's back to prefixing System::
// MyWonderful.cpp
#include "stdafx.h"
#include "MyHeader.h"
#include <WonderCollection>
using namespace System;
using namespace other::nice::stuff;