Visual C++ complains about ambiguous overload

I have some classes, each of them has a static method which I use to create them (I wan't my classes to be created only with new, don't ask why):
1
2
3
4
5
6
7
8
// Base class
public:
  static XGE_GUIElem* create(int x = 0, int y = 0, int w = 0, int h = 0);
// Extends base class
public:
  static XGE_Stage* create(int x = 0, int y = 0);
private/protected:
  using XGE_GUIElem::create;

And Visual C++ compiler complains about call to ambiguous overload when I create a XGE_Stage, even though the parent one is private (or protected, I don't think this is the problem...).
Is there a way I can get it to work?
Last edited on
Line 3,6: both the base class and the derived class version of create have all the parameters defaulted. If you call create with no arguments, the compiler does not know if you mean to call the base class version, or the derived version. Hence, the ambiguous overload error.

Change one of the functions so that at least one of the arguments is required.
@AbstractionAnon Can I overwrite base's create? Cause I don't need it at all, and I would like to keep my parameters as they are...
Last edited on
I can't see a problem. This approach is common and called "virtual constructor".

You need to post a minimal piece of code that demonstrates the build error, and the exact text of the error.

For example, here is some code that demonstrates what I think you are trying to do (this builds fine):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Base class
class XGE_GUIElem
{
public:
	static XGE_GUIElem* create(int x = 0, int y = 0, int w = 0, int h = 0) { return new XGE_GUIElem;  }
};

// Extends base class
class XGE_Stage : public XGE_GUIElem
{
public:
	static XGE_Stage* create(int x = 0, int y = 0) { return new XGE_Stage;  }

};

int main()
{
	XGE_Stage::create();
	return 0;
}


Now that I read your question again I see you mention "ambiguous overload" which implies that both versions of create are in the same class. This is nothing to do with inheritance and overridiing functions. You can't have two versions of create like that in the same class - just use different names depending on their return type.
Last edited on
I believe the problem is the using statement, if removed the code should compile.
@naraku9333 Thanks, This worked! If I undestood correctly what happened, basically:
- if I just create a function with the same name, I'm "overwriting";
- if I add using statement, I'm overloading parent ones, right?
Topic archived. No new replies allowed.