2017-03-06 18 views
-3

私はコンパイラで私のC++を練習していましたが、これ以外のすべてのエラーを解決しました。私のクラスは宣言されていないと言います。私は最初のクラスを宣言しなかった。 21::7:エラー: 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(); 

} 

この

はエラーメッセージである '手先' は宣言されていない 午後九時20分:エラー:あなたが持っている '{' トークン

+2

"class minion :: Enemy"にコロンが1つ多くあります。 – Rene

+0

なぜあなたは空をspinningAttack(){ cout << "どのようなboutこれ!"; } }; voidの代わりにspinningAttack(){ cout << "どのようにbout this!"; }} –

+0

次に、このクラスの手先::敵{ 公共: INT specialAttack(INT X){ INT attackPower = X。 cout << "これを乗せてください!" << attackPower + 6; } };クラスminion :: Enemyに置き換えてください。{ public: int specialAttack(int x){ int attackPower = 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; 
    } 
}; 
関連する問題