简体   繁体   English

c ++接口错误; 标识符“类”未定义

[英]c++ interfaces error; identifier “class” is undefined

Using : VS2010 : New Project -> Windows32 Console App -> Empty Project 使用 :VS2010:新建项目-> Windows32控制台应用程序->空项目

Language : C++ 语言 :C ++

Problem : I'm attempting a simple interface test with two classes that implement the interface through the 1 virtual method. 问题 :我正在尝试使用两个类通过1虚拟方法实现接口的简单接口测试。 I also attempt to use constructors within those two classes to set default values. 我还尝试在这两个类中使用构造函数来设置默认值。 The main problem occurs when attempting to create an object of either class; 尝试创建任一类的对象时都会发生主要问题。 the IDE complains the identifier "class" is undefined. IDE抱怨标识符“ class”未定义。

I have checked tens of other posts with the same error but most of them are errors in instantiation(proper term?) where they use 我检查了其他数十个具有相同错误的帖子,但其中大多数是实例化(专有名词?)中使用的错误

Class obj(); 

instead of 代替

Class obj;

another issue in the other threads was that the header files were not included in main.cpp AND the concrete class(proper term?). 其他线程中的另一个问题是,头文件未包含在main.cpp和具体类(专有名词?)中。 Still other issues involved the ordering of includes(base class should be first), or even improper use of constructors/deconstructors. 还有其他问题涉及includes的排序(基类应该是第一位),甚至不正确地使用构造函数/反构造函数。

I have checked over those issues to see if they would work for me but have been unable to figure out what the problem is. 我检查了这些问题,看它们是否对我有用,但无法弄清楚问题出在哪里。

Any insight would be appreciated! 任何见识将不胜感激!

Error : (all in main.cpp) 错误 :(全部在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

Code : 代码

IPc.h 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 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 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;
}

Edit 编辑

I placed 我放置

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

inside IPc.h (after the base class) and before #endif(also did one for Orc). 在IPc.h内部(在基类之后)和#endif之前(也对Orc执行了一次操作)。 I also changed 我也变了

humHP = hum1->getHP();

to

humHP = hum1.getHP();

Which seems to have cleared up the previous issues(Thanks Mat and Rahul). 这似乎已经解决了以前的问题(谢谢Mat和Rahul)。 I am now getting a 'class' type redefinition error. 我现在收到“类”类型的重新定义错误。 I believe this has to do with the include guards. 我认为这与包括卫兵有关。 Do I need to surround each individual class with its own set of guards in the .h file or have I implemented them improperly perhaps? 我是否需要在.h文件中用每个单独的防护措施包围每个单独的类,或者我是否正确地实现了它们?

Edit2 EDIT2

I placed both class declarations in their own header files with their own guards. 我将两个类声明都使用自己的守护程序放置在自己的头文件中。 This seems to have resolved the issue of " 'class' type redefinition". 这似乎已经解决了“'类'类型的重新定义”的问题。 Though there is now an error of unresolved external symbol on the virtual attack calls for both derived classes. 尽管现在在虚拟攻击上存在两个派生类都无法解析的外部符号的错误。

  1. You are not allowed to use qualified names in class member declarations. 不允许在类成员声明中使用限定名称。 Ie when declaring the constructor of class Human inside class Human , you are not allowed to call it Human::Human() . 即声明类的构造函数时Human类的内部Human ,你不能把它称为Human::Human() It should be just Human() . 它应该只是Human() The same applies to all other methods. 这同样适用于所有其他方法。

  2. Trying to declare method int IPc::Attack() inside other classes makes no sense at all. 试图在其他类中声明方法int IPc::Attack()毫无意义。 It should be just int Attack() . 应该只是int Attack()

  3. You declared classes Human and Orc inside IPc.cpp . 您在IPc.cpp声明了HumanOrc类。 These classes are only visible inside IPc.cpp and nowhere else. 这些类仅在IPc.cpp内部IPc.cpp ,在其他任何地方IPc.cpp可见。 In C++ language class declarations for program-wide classes are typically placed in header files, just how you did it with IPc class. 在C ++语言中,程序级类的声明通常放置在头文件中,就像您使用IPc类所做的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM