ambiguity: same class declared twice

S

silversurfer2025

Hello world,
I seem to go from one tricky error to the other (at least tricky for
me). I am using qt and another code-package where many other files are
stored which I need (written by someone else). The problem is that
within this package and qt there are two definitions of the class
'Region' such that I get the compiler-error:

use of 'Region' is ambiguous
/usr/local/Trolltech/Qt-4.1.4/include/QtGui/qwindowdefs.h:124: error:
first declared as 'typedef struct _XRegion* Region' here
.../../robotcontrol/src/./ImageProcessing/ObjectAnalysis/ChainCoding.h:10:
error: also declared as 'class Tribots::Region' here

I tried explicitly selecting one of the "Regions" by providing a
namespace in front of them e.g. myNameSpace::Region but it did not
work, it still tells me it would be ambiguous... What can I do in such
a situation? Apparently both classes are not written by me and I do not
want to change any of both because it would destroy a lot of
dependencies in other classes...

Someone please help?

Thanks a lot in advance
T.K.
 
H

Howard

silversurfer2025 said:
Hello world,
I seem to go from one tricky error to the other (at least tricky for
me). I am using qt and another code-package where many other files are
stored which I need (written by someone else). The problem is that
within this package and qt there are two definitions of the class
'Region' such that I get the compiler-error:

use of 'Region' is ambiguous
/usr/local/Trolltech/Qt-4.1.4/include/QtGui/qwindowdefs.h:124: error:
first declared as 'typedef struct _XRegion* Region' here
../../robotcontrol/src/./ImageProcessing/ObjectAnalysis/ChainCoding.h:10:
error: also declared as 'class Tribots::Region' here

I tried explicitly selecting one of the "Regions" by providing a
namespace in front of them e.g. myNameSpace::Region but it did not
work, it still tells me it would be ambiguous... What can I do in such
a situation? Apparently both classes are not written by me and I do not
want to change any of both because it would destroy a lot of
dependencies in other classes...

What's "Tribots"? A namespace? Which Region did you want? And what is it,
a type? A member?

I can't tell from that little information exactly what it is that you tried.

You need to show us the code that the error message is referring to. You
should always do this when asking about an error. (See the FAQ @
http://www.parashift.com/c++-faq-lite/ . especially section 5, for what's
expected when posting questions here.)

Namespaces are indeed the proper way to isolate identical symbols from each
other, but without more information, I can't tell where (or even if) they
can be used to fix your problem.

-Howard
 
S

silversurfer2025

Howard said:
What's "Tribots"? A namespace? Which Region did you want? And what is it,
a type? A member?
The Tribots (namespace) Region is a class whereas the one in Qt is
'typedef struct _XRegion* Region' (simply a type-definition?)
I can't tell from that little information exactly what it is that you tried.
I am sorry, but what kind of code should I post? The source-Code from
Qt and the code from the other package?
You need to show us the code that the error message is referring to. You
should always do this when asking about an error. (See the FAQ @
http://www.parashift.com/c++-faq-lite/ . especially section 5, for what's
expected when posting questions here.)
I know the FAQ, but sometimes it is not that easy to find the right
code to post and I thought this was the right amount (even if it was
not compilable)..
Namespaces are indeed the proper way to isolate identical symbols from each
other, but without more information, I can't tell where (or even if) they
can be used to fix your problem.
Thanks anyway, I'll try to give more information...

OK Here we go:
-----------------------------------------------------
The Code from the Region.h:
#include <vector>
#include "../Formation/Image.h"
#include "../../Fundamental/Vec.h"
namespace Tribots {
class Region {
public:
int colClass;

inline Region(int colClass, int x, int y);
inline Region();

int getArea() const;
Vec getCenterOfGravity() const;
double getCompactness() const;

protected:
int area;
Vec centerOfGravity;
};
}
-----------------------------------------------------
ContourMeasure.h (where Region is used):
using namespace Tribots;

class ContourMeasure {
public:
static Tribots::Region* region;
};

PS: I know that there is a double use of Tribots in using namespace and
directly before the type.. This was just a try to beat the compiler
error, which apparently did not worl :(
-----------------------------------------------------
I skip the code from Qt here or should I post it as well? I guess the
line which is important is already in the error message:

/usr/local/Trolltech/Qt-4.1.4/include/QtGui/qregion.h:103: error: use
of 'Region' is ambiguous
/usr/local/Trolltech/Qt-4.1.4/include/QtGui/qwindowdefs.h:124: error:
first declared as 'typedef struct _XRegion* Region' here
.../../robotcontrol/src/./ImageProcessing/ObjectAnalysis/ChainCoding.h:10:
error: also declared as 'class Tribots::Region' here
--------------------------------------------------
Now what I do not understand is that the class ChainCoding (where the
class Region is defined) is part of another namespace as Qt, so why
does the compiler not like it? Furthermore, the error message refers to
files within Qt, so what can I do about that?

Thanks once more, hope this is enough code to make myself clear :p

Greetings and thanks a lot
Tim

PS I do not know how to put compilable code in here because this would
include Qt-Files and the other Package :(
 
T

Thomas J. Gritzan

silversurfer2025 said:
ContourMeasure.h (where Region is used):
using namespace Tribots;

Don't ever write any
"using namespace XXXX" in a header file. It bites!
class ContourMeasure {
public:
static Tribots::Region* region;
};

-----------------------------------------------------
I skip the code from Qt here or should I post it as well? I guess the
line which is important is already in the error message:

/usr/local/Trolltech/Qt-4.1.4/include/QtGui/qregion.h:103: error: use
of 'Region' is ambiguous
/usr/local/Trolltech/Qt-4.1.4/include/QtGui/qwindowdefs.h:124: error:
first declared as 'typedef struct _XRegion* Region' here
../../robotcontrol/src/./ImageProcessing/ObjectAnalysis/ChainCoding.h:10:
error: also declared as 'class Tribots::Region' here
--------------------------------------------------

Well, try to include the qt stuff first.

If it doesn't help, show us the order of includes of the cpp file where
this error messages appears.
 
S

silversurfer2025

Thomas said:
Don't ever write any
"using namespace XXXX" in a header file. It bites!
OK, I'll try. I just used this because in the original file there were
a lot of classes from the Tribots-namespace...
Well, try to include the qt stuff first.
Thank you Thomas, it worked,... wonderful! Still I do not get why
exactly... could you eventually explain?

Greetings
Tim
 
T

Thomas J. Gritzan

silversurfer2025 said:
Thank you Thomas, it worked,... wonderful! Still I do not get why
exactly... could you eventually explain?

You had something like this:
----------
#include "ContourMeasure.h"
#include <qregion.h> // or another header which includes this
....
----------

The compiler sees this, expanded by the preprocessor:

-------------------------------
// in Region.h, included by ContourMeasure.h:
namespace Tribots
{
class Region {};
};

// in ContourMeasure.h, included by your cpp file:
using namespace Tribots;

// code lines in <qregion.h>
....
-------------------------------

The class definition defines Tribots::Region, the using directive puts the
name Region in the global scope.
In qregion.h another Region is declared by typedef, so you have two things
with equal name "Region" in your global scope.

When referenced in qregion.h, the compiler cannot decide which one two use.

Just remove the 'using namespace' from any header and put it - when needed
- in the cpp file _after any #include directives_, or better just use full
qualified names, or simple "using Tribots::Region" where you need it more
frequently.

You can also put the using-directives inside of a class definition or a
function body:

int main()
{
using std::cout;
using std::endl;

cout << "stuff" << endl;
}
 
S

silversurfer2025

Thomas said:
You had something like this:
----------
#include "ContourMeasure.h"
#include <qregion.h> // or another header which includes this
...
----------

The compiler sees this, expanded by the preprocessor:

-------------------------------
// in Region.h, included by ContourMeasure.h:
namespace Tribots
{
class Region {};
};

// in ContourMeasure.h, included by your cpp file:
using namespace Tribots;

// code lines in <qregion.h>
...
-------------------------------

The class definition defines Tribots::Region, the using directive puts the
name Region in the global scope.
In qregion.h another Region is declared by typedef, so you have two things
with equal name "Region" in your global scope.

When referenced in qregion.h, the compiler cannot decide which one two use.

Just remove the 'using namespace' from any header and put it - when needed
- in the cpp file _after any #include directives_, or better just use full
qualified names, or simple "using Tribots::Region" where you need it more
frequently.

You can also put the using-directives inside of a class definition or a
function body:

int main()
{
using std::cout;
using std::endl;

cout << "stuff" << endl;
}

Thanks a lot, now it is way clearer :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top