# Composite object and abstract class

## Recommended Posts

Hey guys, I have an Event class, which is composed of a LandMine class, which is derived from an abstract class called Weapon ...
error C2259: 'LandMine' : cannot instantiate abstract class
class Event
{
private:
string warning;
int warningCount;

public:
void displayWarning();
LandMine EventTime;
};
class Weapon
{
protected:
int damage;
int ammoRemaining;

public:
virtual int attack(int, int) = 0; // damage, ammoRemaining,
virtual int reload(int) = 0; // USER_ammoAmount
virtual void dropAmmo(int) = 0; //USER_ammoAmount
};

class LandMine : public Weapon
{
private:
int size;

public:
int attack(int); // OVERLOADED - Players remaining health
void disarm();

LandMine();

~LandMine();
};
I just can't figure out why on earth this error is coming up. I've put virtual infront of the LandMine constructor too with no go. Ideas? Thanks Using VSC++ express sp1 64bit. EDIT: Source - http://rafb.net/p/dOAhCF93.html [Edited by - 3shirtlessmen on October 11, 2008 6:11:12 PM]

##### Share on other sites
LandMine doesn't provide overrides for all of the pure-virtual members that it inherits, meaning it too is abstract. You need to provide implementations somewhere for the pure virtual attack, reload and dropAmmo functions.

##### Share on other sites
In order to instantiate an object of a child class of an abstract base class, all the pure virtual member functions of that base class must be implemented. In your case LandMine needs to implement
	virtual int attack(int, int) = 0; // damage, ammoRemaining,	virtual int reload(int) = 0; // USER_ammoAmount	virtual void dropAmmo(int) = 0; //USER_ammoAmount

Unless those functions are implemented you can't create a LandMine.

##### Share on other sites
Thanks!

Interesting ... is there a way to ONLY use attack()?

Or is this where clever design comes into play? :)

EDIT: source http://rafb.net/p/dOAhCF93.html

##### Share on other sites
Quote:
 Original post by 3shirtlessmenThanks!Interesting ... is there a way to ONLY use attack()?
You could give the others 'empty' implementations - but don't do that [smile].

Quote:
 Or is this where clever design comes into play? :)
Well, this is where good design comes into play. An interface is part of the specification. You've specified that a weapon can attack, reload and drop ammo. You've further specified that a land mine "is-a" type of weapon. By implication you should be saying that a land mine can attack, reload and drop ammo. Clearly that isn't what you want and it means you've made a mistake with your design.

The immediate flaw I see is the assumption that a weapon is reloadable and has ammo - land mines, knives, swords, rockets and broken bottles are all examples of weapons that don't fit those criteria.

##### Share on other sites
I think I understand.

I start broad with characteristics that ALL weapons have, and spread from there. Each tier down get's more specific. In this case, 2 tiers, a weapons class, and then the weapon.

I could have 2 abstract weapons class, ones that have ammo and those that don't.

EDIT:

Alright, I *thought* I did that ... still the abstract error on 'attack' ... why?

http://rafb.net/p/kaiI3S12.html

##### Share on other sites
int attack(int); is not the same as int attack(int, int);

##### Share on other sites
Oh, I thought overloaded functions would have done that ... why don't they?

##### Share on other sites
Quote:
 Original post by 3shirtlessmenI could have 2 abstract weapons class, ones that have ammo and those that don't.
Weapon and ProjectileWeapon (or RangedWeapon)? That's one way, it'll certainly work.

Quote:
 Alright, I *thought* I did that ... still the abstract error on 'attack' ... why?
Are you suffering from overload versus override syndrome? Note the difference between these:

int Weapon::attack(int, int)

int LandMine::attack(int) // This is an overload NOT an override

You must override virtual functions in order to provide implementations for them.

##### Share on other sites
Quote:
 Original post by 3shirtlessmenOh, I thought overloaded functions would have done that ... why don't they?
The overloaded functions exist along-side other functions of the same name, so int attack(int, int) is still left unimplemented.

##### Share on other sites
Quote:
Original post by dmatter
Quote:
 Original post by 3shirtlessmenI could have 2 abstract weapons class, ones that have ammo and those that don't.
Weapon and ProjectileWeapon (or RangedWeapon)? That's one way, it'll certainly work.

Quote:
 Alright, I *thought* I did that ... still the abstract error on 'attack' ... why?
Are you suffering from overload versus override syndrome? Note the difference between these:

int Weapon::attack(int, int)

int LandMine::attack(int) // This is an overload NOT an override

You must override virtual functions in order to provide implementations for them.

Thanks!

Overriding - in this case, give int attack(int) a body in landmine? I did that - it compiles.

Also, why does it work that way? Reason being,

I assume that all weapons can 'attack' - however, not all weapons attack the same way. Which may lead to convoluted code.

My abstract super class allows for a attack(void); however, with my land mine - I want the size of the landmine to effect the damage, so, I need now, 2 attack functions to have this work ... I don't understand the logic.

##### Share on other sites
Quote:
Original post by 3shirtlessmen
Quote:
 Original post by dmatterint Weapon::attack(int, int)int LandMine::attack(int) // This is an overload NOT an overrideYou must override virtual functions in order to provide implementations for them.

Thanks!

Overriding - in this case, give int attack(int) a body in landmine? I did that - it compiles.
Be sure that attack(int, int) has an implementation too though. You cannot have any unimplemented pure virtual functions if you want to instantiate a class.

Quote:
 Also, why does it work that way? Reason being,I assume that all weapons can 'attack' - however, not all weapons attack the same way. Which may lead to convoluted code.My abstract super class allows for a attack(void); however, with my land mine - I want the size of the landmine to effect the damage, so, I need now, 2 attack functions to have this work ... I don't understand the logic.
Could you explain a little more? Part of the problem may be that I don't know what these integer parameters are that your functions are taking.

Perhaps you want a virtual int damage() = 0; function in the Weapon class, then specific weapons can override that to calculate their inflicting damage however they see fit.

##### Share on other sites
Well, what's a good way to override it? I overloading it (the best way?) won't work, or rather, just complicates things.

##### Share on other sites
Quote:
 I assume that all weapons can 'attack' - however, not all weapons attack the same way.

Please describe, in more detail, your concept of what "attacking" means, and what a "way of attacking" is. Design often requires talking things through.

##### Share on other sites
Quote:
 I want the size of the landmine to effect the damage, so, I need now, 2 attack functions to have this work ... I don't understand the logic.

I don't see why you need 2 attack functions for that.
It is not the attack functions responsibility to set the weapons size is it?
The size of the LandMine sounds more like a LandMine constructor parameter

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628312
• Total Posts
2981999

• 9
• 9
• 13
• 11
• 13