Class prototyping not working

991c178cbb1237d5e8e147aae5782cb9
0
TTTNL 101 Feb 23, 2013 at 23:55 c++

I have two classes and both prototyped/forward declared

class gameClass;
class enemyClass;


class enemyClass
{
public:
    void function
    {
       gameObject.restart();
    }

}enemyObject;


class gameClass
{
public:
    void restart()
    {
          //
    }

}gameObject;

And i still get this error:

error C2065: 'game' : undeclared identifier

error C2228: left of '.restart' must have class/struct/union
1>         type is ''unknown-type''

I am using the template from the introduction to c game development tutorials.

6 Replies

Please log in or register to post a reply.

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 167 Feb 24, 2013 at 02:03

You’ve forward-declared the classes gameClass and enemyClass, but not the variables gameObject and enemyObject. You can do that as follows:

class gameClass;
extern gameClass gameObject;

class enemyClass;
extern enemyClass enemyObject;

By the way, I think it’s generally considered poor style to place a variable definition hanging on the end of a class that way. It is difficult to see (easy to overlook when scanning the code), and also it will cause problems if you later want to put the class definition in a header file to be used by multiple source files. It’s better to separate the class definition from the variable definition, like:

class gameClass
{
    // ...
};

gameClass gameObject;
991c178cbb1237d5e8e147aae5782cb9
0
TTTNL 101 Feb 24, 2013 at 11:29

Ok, i’ve got them prototyped like so:

class playerClass;
extern playerClass player;
class enemyClass;
extern enemyClass enemy[enemies];
class gameClass;
extern gameClass gameObject;

Made the objects

playerClass player;
enemyClass enemy[enemies];
gameClass gameObject;

This is my enemy class:

class enemyBubble
{
public:

void checkCol()
{
  if //collision is true
  {
   respawn();
   if (player.health > 0)
   {
    player.health -= 5;
    player.score -=5;
   }
   else //restart game
   {
    gameObject.restart();
   }
  }
}
};

This is my class gameClass:

class gameClass
{
public:
void restart()
{
player.respawn();
for ( int i=0 ; i<enemies ; i++ )
{
enemy[i].respawn();
enemy[i].speed = 0.1;
}
};

And i still get this error:

1>error C2027: use of undefined type 'gameClass'
1> see declaration of 'gameClass'

the first error refers back to gameObject.restart(), and the second error refers to the prototype of gameClass

I want to have the checkCol funtion in the gameClass as wel, now i have a checkCol for every class. But then i would still need to be able to refer back and forth between classes that are not in the right order. What am i doing wrong?

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 167 Feb 24, 2013 at 18:27

In C++ there is a rule that between classes, you can’t use anything before you have declared it. This doesn’t apply within a class (you can call a method in the same class that you have not declared yet), but it does apply between classes (you cannot call a method in another class that you have not declared yet).

So, for instance, saying “class gameClass;” tells the compiler that “there is a class named gameClass”, and this lets you do a few things with it, such as declaring extern variables of that type. However, you have not yet declared the fields or methods of gameClass, so you cannot yet refer to those.

The usual pattern in C++ is to place in a header file the definitions of all classes with their fields and methods, but not including the bodies of the methods (unless they are very trivial methods). This gets everything declared properly, and then in the source file you place the bodies of the methods, where you’re free to call other methods of any classes, since everything has been declared already.

// In a header file
class gameClass
{
public:
    void restart();    // Note, the body of the function is NOT here
    ...

    int level;    // Fields of the class are defined here too
};

extern gameClass gameObject;

class enemyBubble
{
public:
    void respawn();
    void checkCol();
    ...

    int health;
    float speed;
    ...
};

extern enemyBubble enemy[enemies];



// Now in a source file that #includes the previous header...

// Define the variables we previously declared; this actually creates the variables
gameClass gameObject;
enemyBubble enemy[enemies];

void gameClass::restart()
{
    // Here is the body of the restart method
    player.respawn();
    for (int i = 0; i < enemies; i++)
    {
        enemy[i].respawn();   // We can call this here since enemyBubble::respawn() was declared,
                              // even though its body hasn't been defined yet
        enemy[i].speed = 0.1f;
    }
}

void enemyBubble::checkCol()
{
    ...
    gameObject.restart();   // We can call this here since gameClass::restart() was declared;
                            // its body HAS been defined at this point, but that doesn't matter.
}
991c178cbb1237d5e8e147aae5782cb9
0
TTTNL 101 Feb 26, 2013 at 17:34

Aaaannndd it’s still not working, i’m sorry i tried everything up to my knowledge but i keep getting red lines and errors. I will make a post with examples but it will take some time, here are the 2 errors i get that i think are the main problem

in my classes.cpp, when i hover my mouse over gameClass in “gameClass::restart()”, it says:

“explicit type is missing ( ‘int’ assumed )”

and when i hover my mouse over restart() it says

“declaration is incompatible with “void gameClass::restart()” (declared at line 77 of “d:\file\file\template\classes.h”)

and this is line 77 in classes.h

class gameClass
{
public:
void restart();
};

I will make a better example later this evening but i hope one of you can already guess my problem here.

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 167 Feb 26, 2013 at 18:00

Did you put the “void” in where you define gameClass::restart()? It sounds like maybe you wrote

gameClass::restart()
{
   ...
}

instead of

void gameClass::restart()
{
    ...
}
991c178cbb1237d5e8e147aae5782cb9
0
TTTNL 101 Feb 26, 2013 at 23:16

That seems to be the solution, my bad. I haven’t “converted” all of my classes to classes.cpp yet, so i still get errors but the red lines under the ones i did “convert” are gone! Thank you!