0

I would like to avoid re-definition from two different include files as follows:

File ABC.h

extern int v=1;

File foo.h

#include "ABC.h"
class Foo
#ifdef BLA
: public ABC
#endif

{
    ...
};

File bar.h

extern int v=3;

Main:

#define BLA
#include <foo.h>
#include <bar.h>

Basically foo is a class written by me, and bar is a third-party library. But it doesn't seem to work. How should I solve the problem?

Sorry, it's a bit hard to describe, the example is kind of bad as the conflicted parts are actually not variables, but something like #define and wrapped in big blocks of code (error message is: "multiple definition of `__vector_17'"). Is there a way to solve it without using a namespace?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Xun Yang
  • 4,209
  • 8
  • 39
  • 68

2 Answers2

1

Using a namespace you can solve this problem:

namespace foo
{
    int val =1;
}

namespace bar
{
    int val =3;
}

using namespace foo;

int x = val; //Now x will be assigned with 1
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
shivakumar
  • 3,297
  • 19
  • 28
0

A namespace is what you need:

namespace foo
{
    int v =100;
}

namespace bar
{
    int v =500;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
4pie0
  • 29,204
  • 9
  • 82
  • 118