Jump to content
  • Advertisement
Sign in to follow this  
Rhaal

Trouble with std::stack, structs, and extern

This topic is 4624 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

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

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
Uh, you declared it as TheStack in your header, and trying to declare a stack of an entirely differnt type in your source file.

Share this post


Link to post
Share on other sites
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!

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!