How to solve this error: Public does not name a type?
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();
}
错误:“Public”没有命名类型。引用了Ninja和Monster的public类,结果我无法访问Ninja和Monster的attack()函数
将 Public:
更改为 public:
。
C++ 是区分大小写的语言。 "Public"和"public"是两个完全不同的东西。
#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();
}
错误:“Public”没有命名类型。引用了Ninja和Monster的public类,结果我无法访问Ninja和Monster的attack()函数
将 Public:
更改为 public:
。
C++ 是区分大小写的语言。 "Public"和"public"是两个完全不同的东西。