I am having some trouble with virtual classes with multiple possible return types.
for example lets say i have a virtual class "Gun"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
usingnamespace std;
template<class ammoType>
class Gun
{
public:
virtual ammoType getAmmo() = 0;
};
class Rifle: public Gun
{ // error on this line
public:
ammoType getAmmo() // error on this line
{
...
}
};
with the errors
error: expected class-name before '{' token
error: `ammoType' does not name a type
this is what i've tried so far but the code isn't working for me. So my question is simply what is my problem and how might i fix it? oh and for fun, lets say the ammoType must be able to be anything like a vector or an int or even another rifle. (This is the reason i used templates.)
Gun is a template. In order to make it a class you need to give it a type for 'ammoType'.
Here, you'd probably want to make Rifle a template also:
1 2 3 4 5 6 7 8
template <typename ammoType>
class Rifle : public Gun<ammoType> // derive from Gun<ammoType>, not Gun
{
//...
ammoType getAmmo() // now this is OK
{
}
};