+ 1

Can someone explain me why isnt it working ? ;c

Each time I enter this code I keep getting the error: "cannot convert 'Ninja*' to 'Enemy*' in initialization" "cannot convert 'Monster*' to 'Enemy*' in initialization" #include <iostream> #include <string> using namespace std; class Enemy { public: virtual void attack() { } }; class Monster { void attack() { cout << "Monster - Attack !" << endl; } }; class Ninja { void attack() { cout << "Ninja - Attack !" << endl; } }; int main() { Ninja n; Monster m; Enemy *e1 = &n; Enemy *e2 = &m; e1->attack(); e2->attack(); return 0; }

8th Oct 2017, 3:08 PM
Galagann
3 Respuestas
+ 5
You forgot to inherit. Should be class Ninja : public Enemy { ... } same with Monster.
8th Oct 2017, 3:22 PM
Schindlabua
Schindlabua - avatar
+ 5
This is as the classes Monster and Ninja do not inherit the class Enemy. They are purely unrelated, and thus, implicit conversion is impossible. Try adding ':public Enemy' after Monster and Ninja. Eg : class Monster : public Enemy{ //Code of the class. };
8th Oct 2017, 3:23 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
0
Ooh im so dumb thx guys
9th Oct 2017, 2:51 AM
Galagann