Sign in to follow this  

More C++ errors....

This topic is 4690 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I continue on my quest for a nice way to change game states (and learn some C++ to boot) and I've run across another problem. I've created a State class from which multiple states inherit. The main loop is much simplified by using this scheme. The error I am getting is: g++ -c main.cpp g++ state_machine.o main.o -oStateMachine main.o(.text+0x2a): In function `main': : undefined reference to `Menu::Menu[in-charge]()' collect2: ld returned 1 exit status make: *** [StateMachine] Error 1 The main is pretty simple:
#include<stdio.h>
#include "state_machine.h"

State *current_state;
int game_running;

int main()
{
 current_state = new Menu;

 while(game_running)
 {
  current_state->getInput();
  current_state->update();
  current_state->draw();
 }

 printf("Game Finished\n");

 return 0;

}
Here are the class definitions:
#ifndef _STATE
#define _STATE

extern int game_running;

class State;

extern State *current_state;

class State{

 public:
  State();
  virtual ~State();

  virtual void init()=0;
  virtual void cleanup()=0;

  virtual void pause()=0;
  virtual void resume()=0;

  void changeState(State *next_state )
  {
   current_state->cleanup();
   current_state = next_state;
   current_state->init();
  }

  virtual void getInput()=0;
  virtual void draw()=0;
  virtual void update()=0;
};

class Menu: public State{

 public:
  Menu();
  virtual ~Menu();

  void init();
  void cleanup();

  void pause();
  void resume();

  void getInput();
  void draw();
  void update();

};
#endif
Thanks for the help :)

Share this post


Link to post
Share on other sites
I just noticed that I had nothing implemented for my constructor/deconstructor. I've gone ahead and ammended them by declaring them both inside the class and it compiled. I was under the impression that a virtual class had no need of a constructor definition, but it would seem I am mistaken. I'm assuming that is what you were asking about, The Rug?

I'm still not sure whether this makes it work or not, but it's one step closer...


I might as well also ask... If I where to put a "delete current_state;" line just after the "current_state->cleanup()" line, would this be a potential hazard for my program?

The reason I ask is because changeState() is called from within the class itself, therefore I would be deleting the object from within. After it finished, the function would return to the function which called it which as a method inside the class (getInput() in this case). Would this not be dangerous? When I dereference current_state, does it take care of garbage colletion or would it remain in memory throughout execution? The memory loss would not be great (I only have three states), but I don't like leaving loose ends untied.

[Edited by - TheWanderer on February 10, 2005 1:03:12 PM]

Share this post


Link to post
Share on other sites
I mean, if you declare a function you have to define it unless it is pure virtual. If a class contains a pure virtual function, however, it cannot be instantiated. At current you'll probably still get a linker error because you have other functions that are declared but aren't defined (unless you haven't posted the definitions).

Share this post


Link to post
Share on other sites
I currently have them doing basic output so I know they are working. Here is the code:

#include "state_machine.h"

void Menu::init()
{
}

void Menu::cleanup()
{
}

void Menu::pause()
{
}

void Menu::resume()
{
}

void Menu::update()
{
printf("This is the menu main\n");
}

void Menu::draw()
{
printf("This is the menu main\n");
}

void Menu::getInput()
{
printf("Continue Loop?: ");
scanf("%d",&game_running);
if(game_running == 2)
changeState(new AEditor); //AEditor is another State
}




This is the new Header file

#ifndef _STATE
#define _STATE

#include<stdio.h>

extern int game_running;

class State;

extern State *current_state;

class State{

public:
State(){}
virtual ~State(){}

virtual void init()=0;
virtual void cleanup()=0;

virtual void pause()=0;
virtual void resume()=0;

void changeState(State *next_state )
{
current_state->cleanup();
delete current_state;
current_state = next_state;
current_state->init();
}

virtual void getInput()=0;
virtual void draw()=0;
virtual void update()=0;
};

class Menu: public State{

public:
Menu(){}
virtual ~Menu(){}


void init();
void cleanup();

void pause();
void resume();

void getInput();
void draw();
void update();

};




As you can see, they are simply empty constructors/deconstructors. The only other change is in the changeState function.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

By default, all classes have a constructor and destructor defined. You don't need to do anything for them to be there. However, by actually adding the State(); line to your class, you're saying "I don't want to use the default constructor. I'll write my own." This means you'll need to write the constructor yourself. If you take State(); out of your class definition, the program will use the default constructor.

Share this post


Link to post
Share on other sites

This topic is 4690 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this