Visual Studio error C2061:语法错误:标识符。

时间:2022-11-09 00:41:45
//Species.h
#pragma once
#include <iostream>
#include <string>
using namespace std;
class Species
{
protected:
    int HP, GroundAttack, AirAttack, MoveMode;
public:
};

// Protoss.h
#pragma once
#include "Species.h"
#include "Terran.h"

class Protoss : public Species
{
protected:
    int PS;
public:
    virtual void Input();
    virtual void Output();
    void B_AbilityAttack_A(Terran *);
};

// Terran.h
#pragma once
#include "Species"
#include "Protoss.h"

class Terran : public Species
{
public:
    virtual void Input();
    virtual void Output();
    void A_AbilityAttack_B(Protoss *);
};

"error C2061: syntax error : identifier 'Terran'"

错误C2061:语法错误:标识符“Terran”

"error C2061: syntax error : identifier 'Protoss'"

错误C2061:语法错误:标识符“Protoss”

error in: void A_AbilityAttack_B(Protoss *) and void B_AbilityAttack_A(Terran *)

错误:void A_AbilityAttack_B(Protoss *)和void B_AbilityAttack_A(Terran *)

How to fix it?

如何修复它吗?

After fix In a method of class Protoss, I wrote:

修正了一种类神族的方法后,我写道:

void Protoss::B_AbilityAttack_A(Terran *x)
{
    if (this->AbilityAttack() == 0 && x->GetMoveMode() == 0)
    {
        x->SetHP(x->GetHP() - this->GAttack());
    }
    else if (this->AbilityAttack() == 1 && x->GetMoveMode() == 0)
    {
        x->SetHP(x->GetHP() - this->GAttack());
    }
    else
    {
        x->SetHP(x->GetHP() - this->AAttack());
    }
}

And error in x : "pointer to incomplete class type is not allowed"

以及x中的错误:“不允许指向不完全类的指针”

So how to fix it?

那么如何解决这个问题呢?

1 个解决方案

#1


2  

You have a circular dependency. Remove the include of Protoss by Terran and vice versa, the use forward declarations instead.

你有一个循环依赖。通过Terran来移除神族的包含,反之亦然。

// Protoss.h
#pragma once
#include "Species.h"

class Terran;
class Protoss : public Species
{
protected:
    int PS;
public:
    virtual void Input();
    virtual void Output();
    void B_AbilityAttack_A(Terran *);
};

// Terran.h
#pragma once
#include "Species"

class Protoss;
class Terran : public Species
{
public:
    virtual void Input();
    virtual void Output();
    void A_AbilityAttack_B(Protoss *);
};

This will work because you don't need the full class definitions because the arguments are pointers, you just need to forward declare them.

这将起作用,因为您不需要完整的类定义,因为参数是指针,您只需要向前声明它们即可。

#1


2  

You have a circular dependency. Remove the include of Protoss by Terran and vice versa, the use forward declarations instead.

你有一个循环依赖。通过Terran来移除神族的包含,反之亦然。

// Protoss.h
#pragma once
#include "Species.h"

class Terran;
class Protoss : public Species
{
protected:
    int PS;
public:
    virtual void Input();
    virtual void Output();
    void B_AbilityAttack_A(Terran *);
};

// Terran.h
#pragma once
#include "Species"

class Protoss;
class Terran : public Species
{
public:
    virtual void Input();
    virtual void Output();
    void A_AbilityAttack_B(Protoss *);
};

This will work because you don't need the full class definitions because the arguments are pointers, you just need to forward declare them.

这将起作用,因为您不需要完整的类定义,因为参数是指针,您只需要向前声明它们即可。