0

I'm writing a chess program with wxWidgets. At one point, I have a subclass of wxGLCanvas, and it makes use of a Move class I wrote. Unfortunately, it seems like there's a method wxWindowBase::Move(), and so all my statements of the form list<Move> li won't compile.

Is there any nice way to resolve this? I've tried list< ::Move> li, and that does fix the problem, but it's gross, and I have to make that change everywhere. Unlike with namespace conflicts, a using-declaration doesn't seem to help here.

1
  • What using declaration (or equivalently, typedef) did you try? Commented Dec 30, 2014 at 6:41

2 Answers 2

3

There are a few ways to disambiguate a base class hidden by a method name.

typedef Move::Move Move_Base; // 1. the LHS of :: operator ignores functions using typename Move::Move; // 2. non-template "typename" avoids constructor typedef class Move Move_Base; // 3. elaborated type specifier typedef ::Move Move_Base; // 4. namespace qualification (as mentioned) 

(1) may not work in GCC due to a bug. (Not sure; you can try and see.)

(2) is perhaps the most elegant, but the compiler has to get some C++11 nuances right. The typename keyword there is illegal in C++03, but I think it's not necessary. In C++11, the syntax refers to the constructor instead of the type name unless you say typename.

You'll need to qualify the inherited function as wxWindowBase::Move().

(3) only works when the base class is part of the current namespace, which often it shouldn't be.

(4) is a bit of a pain because a class hard-coded against its enclosing namespace needs modification if moved to another namespace. As you mentioned, a bit ugly.

Sign up to request clarification or add additional context in comments.

2 Comments

No permutation of 2) seems to work, but somehow I forgot about typedefs, which will more-or-less fix my problem. Thank you!
@HenrySwanson Hmm, GCC accepts the declaration, but Clang doesn't, which looks like a bug. However, GCC still doesn't interpret such a declaration as resolving the ambiguity, which is another bug.
1

Put your code into a namespace. Then your class will be something::Move and it won't conflict as easily.

6 Comments

That seems like the same problem though; it's just a difference of which namespace it's in. I like being able to have using something::Move in my .cpp files, so things like std::list<something::Move> become list<Move>, which is much more readable, IMO.
I don't understand: if your code is in namespace something, then you can say list<Move> without using it.
I can, but it still mistakes Move the class for Move the method.
@HenrySwanson: maybe you should just rename your class and sidestep this mess.
I could, but what else would I call a class that stores 'this is where the piece went and if it captured'? I was just hoping there was some way of making it pretty; I can live with the extra ::.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.