Trouble with std::stack, structs, and extern

Started by
6 comments, last by Rhaal 18 years, 5 months ago
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.
- A momentary maniac with casual delusions.
Advertisement
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;
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
- A momentary maniac with casual delusions.
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?
Ok here's a minimal program

..wait.. I messed up
- A momentary maniac with casual delusions.
Uh, you declared it as TheStack in your header, and trying to declare a stack of an entirely differnt type in your source file.
I must have some other problem going on. I'm gonna try to narrow this down a little more. Thanks for the help so far!
- A momentary maniac with casual delusions.
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.
- A momentary maniac with casual delusions.

This topic is closed to new replies.

Advertisement