cannot declare parameter "attacker" to be of abstract type "Mob" I need help | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

cannot declare parameter "attacker" to be of abstract type "Mob" I need help

So in this code I have two subclasses (zombie and goblin) which inherit the base class of Mob, I am trying to add an attack method so both of the subclasses can attack each other. https://code.sololearn.com/csI5BgO7W3xM/?ref=app Ask me questions if I didn't explain enough. Also I'm not looking for someone to fix it for me rather someone to point me into the direction of what I am doing wrong so I can fix it myself

24th Apr 2019, 2:07 AM
ScriptingEngine
ScriptingEngine - avatar
6 Answers
+ 6
In your Mob class try to pass the argument of the attack function by reference, instead of by value. Like this: virtual void attack(Mob &target) = 0; It's because when passing by value, the compiler tries to instantiate the class but it's not possible because it is abstract. Pass by reference, works around this issue. https://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/ I would use only one argument here (target) because the attacker is pretty obvious (the instance that calls the function).
24th Apr 2019, 4:59 AM
Tibor Santa
Tibor Santa - avatar
+ 5
Another thing I would add is a virtual destructor to Mob. When your class has the potential to be used polymorphically ( when you have at least 1 virtual function ) you should provide a virtual destructor. Otherwise when you do use polymorphism your classes aren't destructed correctly and you risk memory leaks and 'zombie' objects. Also I suggest that you use the constructor's initialization list instead. Currently your variables are initialized and then assigned again. With the initializer list you skip the assignment part which is potentially expensive.
24th Apr 2019, 7:45 AM
Dennis
Dennis - avatar
+ 5
Seems to work now.
24th Apr 2019, 9:32 AM
Sonic
Sonic - avatar
+ 1
Tibor Santa thanks that worked :)
24th Apr 2019, 6:51 AM
ScriptingEngine
ScriptingEngine - avatar
+ 1
Dennis will do, i added that stuff to my Todo list
24th Apr 2019, 9:11 AM
ScriptingEngine
ScriptingEngine - avatar
0
Sonic Yes, tibor's answer solved my problem
24th Apr 2019, 9:36 AM
ScriptingEngine
ScriptingEngine - avatar