2017-03-06 126 views
-3

我在编译器中练习我的C++,并且我解决了所有的错误,但是这个错误。它说我的班级没有申报。我甚至没有宣布我有第一堂课。C++错误说类没有声明

// Example program 
    #include <iostream> 
    #include <string> 
    using namespace std; 

    class Enemy{ 
private: 
int attack = 0, 
block = 0; 

public: 
void chargedAttack(){ 
    cout << "Get Ready!"; 
    } 
void spinningAttack(){ 
    cout << "How bout this!"; 
    } 
}; 

class minion::Enemy{ 
public: 
    int specialAttack(int x){ 
    int attackPower = x; 
    cout << "Take this you chump! " << attackPower + 6; 
    } 
}; 


int main() 
{ 
    minion chump1; 
    chump1.spinningAttack(); 

} 

这是错误消息:21:7:错误: '奴才' 还没有被宣布 21:20:错误:在 '{' 令牌

+2

在“class minion :: Enemy”中,有一个冒号太多。 – Rene

+0

你为什么会出现void spinningAttack(){cout <<“这是怎么回事!”; } };而不是无效的旋转攻击(){ cout <<“这是怎么回事!”; } } –

+0

然后这个类minion :: Enemy { public: int specialAttack(int x){attackerPower = x; cout <<“拿你这个笨蛋!”<< attackPower + 6; } };用class minion替换::敌人{ public: int specialAttack(int x){attackerPower = x; cout <<“拿你这个笨蛋!”<< attackPower + 6; } } –

回答

0

的你有问题有望不合格-ID是在你的minion类:

class minion::Enemy{ 
public: 
    int specialAttack(int x) 
    { 
     int attackPower = x; 
     cout << "Take this you chump! " << attackPower + 6; 
    } 
}; 

为了声明一个子类,它需要有语法如下:

class ChildClass : public ParentClass 
{ 
    // Declare variable and/or functions 
}; 

您正在声明一个新的类,从而混淆了在其类的外部实现函数的语法。您可以在其类之外实现以下功能:

void AnyClassFunction::AnyClass { 

但是,不能使用相同的语法来声明子类。您应该将minion类的标题行更改为:

class minion : public Enemy{ // This tells the compiler that the minion class is inherited from the Enemy class 
public: 
    int specialAttack(int x) 
    { 
     int attackPower = x; 
     cout << "Take this you chump! " << attackPower + 6; 
    } 
};