Let’s say I have three classes – Animal, Cat and Dog, where Cat and Dog are subclasses of Animal (this does sound like the first lectures, but it’s not homework I promise, just simplifying the real code)
Dog* spike = new Dog();
Cat* puss = new Cat();
int main(int argc, char** argv)
{
function(spike, puss);
return 0;
}
void function(Animal *pet, Animal *pet2)
{
magic->andSoForth();
}
Now this generates the following error:
Cannot convert parameter 1 from 'Dog *' to 'Animal'
No constructor could take the source type,
or constructor overload resolution was ambiguous
Changing the parameters to exactly match generates similar errors, only that it says it can’t convert from a class to the same class.
I have successfully called the subclasses functions and members that they inherit from the superclass, so I know that this, logically, should work. I just don’t know in what twisted way this language want me to bend logic.
EDIT
Solution happen to be: pointers confuse everyone.
- Declare pointers.
- Send pointers as arguments to a function that does NOT handle pointers.
In my example, I sent the “not-pointers” to the function that wanted pointers, I just switched that. Now it works fine.
This is how it should look.
Tested and working.