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

Using: VS2010 : New Project -> Windows32 Console App -> Empty Project

Language: C++

Problem: I'm attempting a simple interface test with two classes that implement the interface through the 1 virtual method. 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.

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?). Still other issues involved the ordering of includes(base class should be first), or even improper use of constructors/deconstructors.

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)

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

#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;
}

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). I also changed

humHP = hum1->getHP();

to

humHP = hum1.getHP();

Which seems to have cleared up the previous issues(Thanks Mat and 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?

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.

3

1 Answer

  1. You are not allowed to use qualified names in class member declarations. I.e. when declaring the constructor of class Human inside class Human, you are not allowed to call it Human::Human(). It should be just Human(). The same applies to all other methods.

  2. Trying to declare method int IPc::Attack() inside other classes makes no sense at all. It should be just int Attack().

  3. You declared classes Human and Orc inside IPc.cpp. These classes are only visible inside IPc.cpp and nowhere else. In C++ language class declarations for program-wide classes are typically placed in header files, just how you did it with IPc class.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like