Information
I have a class RenderWindow that inherit from RenderTarget2D and RenderTarget3D:
class API RenderWindow : public RenderTarget2D, public RenderTarget3D
Both RenderTarget2D and RenderTarget3D have a function draw, they both have specialization of the function. For example:
class RenderTarget2D
{
public:
void draw(Drawable2D);
void draw(Vertex2D);
}
class RenderTarget3D
{
public:
void draw(Drawable3D);
void draw(Vertex3D);
}
Their are no identical function definition from
RenderTarget2DandRenderTarget3D.
DrawableXD is an interface and VertexXD is a concret class.
Problem:
When using RenderWindow::draw with a class inheriting from Drawable2D (for example, doesn’t work with any specialization type), I have the following error:
error: request for member 'draw' is ambiguous
And provide me with all the possible specialization of the function, like:
note: candidates are: void RenderTarget3D::draw(Vertex3D)
note: void RenderTarget3D::draw(Drawable3D)
note: void RenderTarget2D::draw(Vertex2D)
note: void RenderTarget2D::draw(Drawable2D)
...
I wan’t my compiler to select the correct type specialization for the function, but as I understood, because RenderWindow is derived from both RenderTarget2D and RenderTarget3D the compiler isn’t able to choose the correct function.
Question:
How can I keep this 2 separted class and make the function specialization work? Or if the issue really come only from the fact that I use 2 inherited class to perform the specialization, what kind of change am I looking for?
# EDIT
Minimal reproduciable example:
#include <vector>
#include <array>
#include <iostream>
class RenderTarget2D
{
public:
void draw(int a)
{
std::cout << "2D | int" << std::endl;
}
void draw(std::vector<int> çarg)
{
std::cout << "2D | vector" << std::endl;
}
};
class RenderTarget3D
{
public:
void draw(float a)
{
std::cout << "3D | float" << std::endl;
}
void draw(std::array<float, 3> çarg)
{
std::cout << "3D | array" << std::endl;
}
};
class RenderWindow : public RenderTarget2D, public RenderTarget3D
{
};
int main()
{
RenderWindow win{};
win.draw(1);
return 0;
}
>Solution :
You can’t "mix-in" overloads like that.
You can solve it by bringing all functions into RenderWindow‘s scope:
class API RenderWindow : public RenderTarget2D, public RenderTarget3D
{
public:
using RenderTarget2D::draw;
using RenderTarget3D::draw;
/// ...
};
Or, you know, use different names.