I'm trying to create a base class. I'd like to be able to instantiate the class several different ways but am getting the 'overload is ambiguous error'
Here's a simple class to illustrate. I'd like to be able to instantiate Shapes that either have width/heights or radius
class Shape {
protected:
float width, height, radius;
public:
Shape( float r=0) {
radius = r;
}
Shape( float a=0, float b=0) {
width = a;
height = b;
}
// pure virtual function
virtual float area() = 0;
};
class Circle: public Shape{
public:
Circle(float r=0): Shape(r) {}
float area();
};
class Rectangle: public Shape{
public:
Rectangle( float a=0, float b=0):Shape(a, b) { }
float area();
};
When "Circle" builds off of "Shape", I get the following error:
Circle.h:7: error: call of overloaded 'Shape(float&)' is ambiguous
shape.h:12: note: candidates are: Shape::Shape(float, float)
shape.h:9: note: Shape::Shape(float)
shape.h:5: note: Shape::Shape(const Shape&)
Of course, if I change one of the class constructors to be "int" and leave the other "float", it works. For example this works but at the expensive of not having the type I want for one of the constructors:
class Shape {
protected:
float width, height;
int radius;
public:
Shape( int r=0) {
radius = r;
}
Shape( float a=0, float b=0) {
width = a;
height = b;
}
Question: How do I build the base class overload constructors so that both (or many) can share the same numeric type?