Freya Ren
Freya Ren

Reputation: 2164

how do I solve namespace conflict in C++?

I use Win7 system with Visual Studio 2010. I'm using OpenCV and there is a embedded flann namespace.

But I also used an original FLANN packagehttp://people.cs.ubc.ca/~mariusm/index.php/FLANN/FLANN.

I follow the user manual to compile the FLANN by using

> "C:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"
> cd flann-x.y.z-src
> mkdir build
> cd build
> cmake -G "NMake Makefiles" ..

I added the FLANN folder in my VS2010 project: c++ folder->include directories

So when I write C++ code and want to using "flann" namespace, there is an namespace conflict error.

How could I solve the problem?

Upvotes: 3

Views: 585

Answers (2)

Evgeny Panasyuk
Evgeny Panasyuk

Reputation: 9199

Probably most reliable solution is to rename one of namespaces. You said that you have compiled flann - in a such case, you can rename it's namespace, and recompile again.

Maybe you can reuse Boost.BCP tool with small modification - it has namespace rename feature.

Or if you see some common pattern in flann - then you can try to rename it just by simple global search-and-replace. After renaming, do diff to be sure that only needed parts are renamed, and do grep to be sure that all needed places are renamed.


Another option is to define preprocessor macro during compilation of flann which would rename namespace, like:

#define flann flann_vanilla

and add it to compiler options like:

-Dflann=flann_vanilla

At this case, you should wrap inclusion of every flann header to:

#define flann flann_vanilla
#include "flann.hpp"
#undef flann

Also, if those duplicated namespaces are from same library (maybe different versions) - be aware of possible preprocessor macro clash, especially clash of include guards. Easiest solution for include guards clash is to use implementation-specific #pragma once.

Upvotes: 2

David
David

Reputation: 6571

Wrap the existing 'flann' namespace in a new namespace, then refer to it using fully qualified syntax.

Upvotes: 0

Related Questions