C ++接口错误;标识符 "class" 未定义

c++ interfaces error; identifier "class" is undefined

使用:VS2010:新建项目 -> Windows32 控制台应用程序 -> 空项目

语言:C++

问题:我正在尝试使用两个 classes 进行简单的接口测试,它们通过 1 虚方法实现接口。我还尝试在这两个 class 中使用构造函数来设置默认值。尝试创建 class 的对象时会出现主要问题; IDE 抱怨标识符 "class" 未定义。

我检查了其他数十篇有相同错误的帖子,但其中大部分是在实例化(正确的术语?)中使用

的错误
Class obj(); 

而不是

Class obj;

其他线程中的另一个问题是头文件未包含在 main.cpp 和具体的 class(正确的术语?)中。还有其他问题涉及包含的顺序(base class 应该在第一位),甚至 constructors/deconstructors 的不当使用。

我已经检查了这些问题,看看它们是否适合我,但一直无法弄清楚问题是什么。

如有任何见解,我们将不胜感激!

错误:(全部在main.cpp)

Error   1   error C2065: 'Human' : undeclared identifier 6  1   interfaceGameTest
Error   2   error C2146: syntax error : missing ';' before identifier 'hum1' 6  1   interfaceGameTest
Error   3   error C2065: 'hum1' : undeclared identifier 6   1   interfaceGameTest
Error   4   error C2065: 'Orc' : undeclared identifier  7   1   interfaceGameTest
Error   5   error C2146: syntax error : missing ';' before identifier 'orc1' 7  1   interfaceGameTest
Error   6   error C2065: 'orc1' : undeclared identifier 7   1   interfaceGameTest
Error   7   error C2065: 'hum1' : undeclared identifier 10  1   interfaceGameTest
Error   8   error C2227: left of '->getHP' must point to class/struct/union/generic type 10 1   interfaceGameTest

代码:

IPc.h

#ifndef IPC_H_HAS_BEEN_INCLUDED
#define IPC_H_HAS_BEEN_INCLUDED

class IPc {
  private:
    int hp;
    int mana;
    int endurance;

  public:
    void setHP(int h) { hp = h; }
    void setMP(int m) { mana = m; }
    void setEnd(int e) { endurance = e; }

    int getHP() { return hp; }
    int getMP() { return mana; }
    int getEnd() { return endurance; }

    virtual int Attack() = 0;
};
#endif

IPc.cpp

#include "IPc.h"

class Human: public IPc
{
    public:
        Human::Human()
        {
            this->setHP(10);
            this->setMP(5);
            this->setEnd(10);
        }
        Human::~Human(){}

        int IPc::Attack() // I have tried just "int Attack()" as well
        {
            return 1;
        }
};

class Orc: public IPc
{
    public:
        Orc::Orc()
        {
            this->setHP(20);
            this->setMP(0);
            this->setEnd(20);
        }
        Orc::~Orc() {}

        int IPc::Attack()
        {
            return 5;
        }
};

main.cpp

#include <iostream>
#include "IPc.h"

int main()
{
    Human hum1; // error Human undefined
    Orc orc1; // error Orc undefined
    int humHP = 0;

    humHP = hum1->getHP();

    std::cout << "Human HP is: " << humHP << std::endl;
    return 0;
}

编辑

我放了

class Human: public IPc { virtual int Attack() };

在 IPc.h 内(在基地 class 之后)和 #endif 之前(也为 Orc 做了一个)。我也改了

humHP = hum1->getHP();

humHP = hum1.getHP();

这似乎解决了之前的问题(感谢 Mat 和 Rahul)。我现在收到 'class' 类型重定义错误。我相信这与包含警卫有关。我是否需要在 .h 文件中用自己的一组守卫包围每个人 class,或者我可能没有正确实施它们?

编辑2

我将两个 class 声明放在它们自己的头文件中,并带有它们自己的保护。这似乎解决了“'class' 类型重新定义”的问题。尽管现在在派生的 classes.

的虚拟攻击调用中存在未解析的外部符号错误
  1. 您不能在 class 成员声明中使用限定名称。 IE。在classHuman中声明classHuman的构造函数时,不允许调用Human::Human()。它应该只是 Human()。这同样适用于所有其他方法。

  2. 试图在其他 class 中声明方法 int IPc::Attack() 是毫无意义的。应该只是 int Attack().

  3. 您在 IPc.cpp 中声明了 classes HumanOrc。这些 classes 仅在 IPc.cpp 内部可见,在其他地方不可见。在 C++ 语言中,程序范围的 classes 的 class 声明通常放在头文件中,就像您使用 IPc class.