Search code examples
c++polymorphismpublic

How to solve this error: Public does not name a type?


#include <iostream>

using namespace std;
class Enemy{
    protected:
    int attackPower;
    public:
        void setAttackPower(int a){
        attackPower=a;
        }
};

class Ninja: public Enemy{
    **Public:**
    void attack()
    {
        cout<< "I am ninja chop!" << attackPower<< endl;
    }
};

class Monster: public Enemy{// step 4,Ninja inherits enemy
    **Public:**
    void attack()
    {
        cout<< "Monster must eat you!!!" << attackPower<< endl;
    }
};
int main(){
Ninja n; 
Monster m;

Enemy *enemy1 = &n;
Enemy *enemy2= &m;

enemy1->setAttackPower(29);
enemy2->setAttackPower(99);
n.attack();
m.attack();
}

Error: 'Public' does not name a type. It refers to the public of both Ninja and Monster classes, In result I cant access the attack() function in both Ninja and Monster


Solution

  • Change Public: to public:.

    C++ is a case sensitive language. "Public" and "public" are two completely different things.