C 错误说未声明类

C++ error says class not declared

本文关键字:未声明 错误      更新时间:2023-10-16

我在编译器中练习我的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:错误:尚未声明'Minion'21:20:错误:" {'token

"之前预期的无限制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; 
    }
};