+ 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; }
3 Antworten
+ 5
You forgot to inherit. Should be
class Ninja : public Enemy {
...
}
same with Monster.
+ 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.
};
0
Ooh im so dumb thx guys