Sign in to follow this  
Rhaal

Trouble with std::stack, structs, and extern

Recommended Posts

If I do this, I get a compiler error in main.cpp saying `gStateStack' undeclared (first use this function).
// GameStates.h
#include <stack>
struct gStateStruct { void (*StatePointer)(); };
std::stack<gStateStruct> gStateStack;
// main.cpp
#include <stack>
#include "GameStates.h"
using namespace std;
int main(int argc, char **argv)
{
  Init();

  // Game loop.
  while(!gStateStack.empty())
  {
    gStateStack.top().StatePointer(); // ERROR
  }

  Shutdown();

  return 0;
}


I don't understand. By including GameStates.h, shouldn't it be declaring gStateStack in main.cpp? Edit: Oh yeah, externs. I got everything to compile by using 'extern std::stack gStateStack;' in main.cpp but then I got a linker error.

Share this post


Link to post
Share on other sites
Have you actually placed the gStateStack instance anywhere?
// GameStates.h
#include <stack>

struct gStateStruct { void (*StatePointer)(); };
extern std::stack<gStateStruct> gStateStack;
// GameStates.cpp, main.cpp or anywhere really, as long as you place it exactly one compilation unit.
#include "GameStates.h"

std::stack<gStateStruct> gStateStack;

Share this post


Link to post
Share on other sites
Quote:
Original post by doynax
Have you actually placed the gStateStack instance anywhere?
// GameStates.h
#include <stack>

struct gStateStruct { void (*StatePointer)(); };
extern std::stack<gStateStruct> gStateStack;
// GameStates.cpp, main.cpp or anywhere really, as long as you place it exactly one compilation unit.
#include "GameStates.h"

std::stack<gStateStruct> gStateStack;


If I do that, I get "3 C:\projects\lorium\src\gamestates\gsGameStates.cpp expected constructor, destructor, or type conversion before '<' token " in gsGameStates.cpp

Share this post


Link to post
Share on other sites
Quote:
Original post by Rhaal
If I do that, I get "3 C:\projects\lorium\src\gamestates\gsGameStates.cpp expected constructor, destructor, or type conversion before '<' token " in gsGameStates.cpp
That's odd.. Can you strip it down to a minimal example and post the entire thing?

Share this post


Link to post
Share on other sites
Get this! The whole time I was editing one GameStates.h while compiling a different GameStates.h. The one that I was editing wasn't being compiled in, just open in my IDE.

[lol]

I discovered this by trying to create a new minimal program with copies of some of my source files. I'm so happy! I thought I was going crazy.

Share this post


Link to post
Share on other sites

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