I'm getting confused about the scope of 'using namespace' declarations... hoping someone can clear this up for me!
I'm using two libraries (OpenCV and Ogre3D). I have two cpp files, one uses exclusively OpenCV (PoseEstimator.cpp) and the other exclusively Ogre3D (OgreLogic.cpp).
The top of OgreLog开发者_如何学JAVAic.cpp looks like this:
#include "stdafx.h"
#include "PoseEstimator.h"
#include "OgreLogic.h"
using namespace Ogre;
And the top of PoseEstimator.cpp looks like this:
#include "StdAfx.h"
#include "PoseEstimator.h"
using namespace cv;
using namespace std;
This 'using namespace cv' is the only occurrence in the whole project (I double checked by doing a search). The are no 'using namespace's in headers, only ever in cpp files.
However, when compiling OgreLogic.cpp I get ambiguity errors, e.g.:
cxmat.hpp(3465) : error C2872: 'uchar' : ambiguous symbol 1> could be 'd:\libraries\opencv2.1\include\opencv\cxtypes.h(154) : unsigned char uchar' 1> or
'd:\libraries\ogresdk\include\ogre\OgrePrerequisites.h(106) : Ogre::uchar'
I seem to be misunderstanding something, because I think this should be OK?
Any help greatly appreciated!
Thanks,
Jack
It looks like what is happening is you have a line declaring a uchar
, e.g. uchar x = 12
or something. At the top of your file, you've specified that you're using the Ogre namespace. The compiler is running into a problem determining which uchar
you're using - the cxtypes.h one or the Ogre::uchar.
Try taking out the using namespace Ogre;
and do your function calls as Ogre::doStuff()
to remove ambiguity.
The root problem is that the uchar
in cxtypes.h is not in the cv namespace. The uchar
in the OrgePrerequisites.h is in the Orge namespace (hence Ogre::). By adding using namespace Ogre;
you are actually making any reference to uchar
ambiguous.
As spbots noted, you can remove the namespace usage to solve your problem, but I wanted to address your root question about namespaces. The answer/issue is that the other uchar (the cxtypes one) isn't in a namespace at all. It is simply declared in the header file outside of any namespace.
精彩评论