Can someone explain me why isnt it working ? ;c | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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 Answers
+ 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
Kinshuk Vasisht
Kinshuk Vasisht - avatar
0
Ooh im so dumb thx guys
9th Oct 2017, 2:51 AM
Galagann