//Main.cpp
main()
{
if (!quit)
{
while( handleing_events )
{
CurrentState->handleEvents();
}
CurrentState->Logic();
checks_if_the_state_changed();
CurrentState->Render();
}
close stuff
}
//States.cpp
#include states.h
...
//I initialize everything right here
Multiplayer::Multiplayer()
{...}
Multiplayer::handleEvents()
{
//I need access to chat_client c
}
Multiplayer::Logic()
{...}
Multiplayer::Render()
{...}
Multiplayer::~Multiplayer()
{
//I need access to chat_client c & boost::thread t
}
I tried to put the stuff in extern at states.h, and initialize it at Multiplayer(), but in handleEvents & ~Multiplayer, it says "c" is not defined...
Sorry that I can't give anything to compile, I have < 10 000 lines of code :/
If it is alright for the networking to end every time you exit the 'Multiplayer' state, setting it as a local variable will cause it to be initialized upon the construction of a 'Multiplayer' instance. Alternately, you could have 'c' as a pointer (preferably std::unique_ptr or the boost version) and then simply dynamically allocate it, causing it to start when you need to. When you are done you can either just let the pointer fall out of scope (assuming its a unique_ptr) or manually deallocate it, to stop it consuming resources.
For defining it extern, all that does is say that it is defined in another compilation unit - you still need to have it defined somewhere (and thus initialized) at namespace scope. Though, another option would be if chat_client has the option for a default constructor and then an initializer, which would be easier than the pointer approach.
Also, I think you meant "> 10000 lines of code" :)
Well.. I make the un allocated pointer as a global variable.. std::unique_ptr<chat_client> c;
And then I made it allocate in the server state... std::unique_ptr<chat_client> c(new chat_client( io_service, iterator));
I start the program and yep, it did not initialize...
but when I start the server, it also wont initialize,
and when I try to send a message the game would crash D:
Apparently it seems to be a problem coming the function called write(). It would normally work if I constructed it as not a pointer, but when I molded it to the pointer function operator: c.write(...); -> c->write(...);
Oh sorry, I only just checked my 'My Topics' again... well, now that you've spent so much time on this at least you won't forget it. Though, partly the reason was you didn't give us the context to the call, so we didn't know that you weren't checking the validity of the pointer. Just for future reference.
I don't want to bump this but...
The validity wasn't the problem...
It seemed like at the time when I did that it made it compiled without errors or run time problems... but it didn't actually work.
I found the real solution shortly after:
1 2 3 4 5 6
//Define the undefined :O
std::unique_ptr<chat_client> cc(new chat_client( io_service, iterator)) ;
c = std::move(cc);
std::unique_ptr <boost::thread> tt( new boost::thread(boost::bind(&boost::asio::io_service::run, &io_service)));
t = std::move(tt);