This is my first post here and I believe it to be posted in the right forum however I apologize if it is not.
I have function in a header file that creates numerous object instances.
This header file is included in the main source file and when the aforementioned function is called it produces the objects as required, however upon completion and the focus is returned to the main source file the instance variables are no longer accessible to me.
Is anyone able to suggest why this is?
I'm from less complex programming period and assume what I'm trying to do is correct but I could be wrong, if more information is required just let me know.
That's too little information. At least the function in question and its invocation is needed, plus an explanation of what "not accessible" is supposed to mean.
When doing a step by step run-through GenerateNPCs(2); amongst other things gives Plyr[x].vision a random value, however when after we hit the Return 0; the debugger gives me the following error;
Plyr[0].vision CXX0017: Error: symbol "Plyr" not found
By inaccessible I mean that once the function is completed and returns to the next step I can no longer access the Instance variables created.
I'm not sure if what I want is possible but after calling GenerateNPCs (and its completion) I would like all object instances and associated variables to remain accessible for manipulation.
Well. What happens when calling GenerateNPCs is that 100 NPCs are created and two are being initialized (probably). The function then exits and the NPC array goes out of scope - all 100 are destroyed.
So you need to make sure your NPCs survive.
You can either return the NPCs you created or add them to a vector that you pass by reference.
Thanks for your replies, appreciate it - I assumed they were being destroyed but wasn't able to locate any information online that I could conclusively tie-in to my problem.
I'll see if I can utilize your suggestion this evening.
If you pass the vector by reference to the function, you can have the function modify a vector you give it. What wtf suggested is simply put the array in global scope, where you will be able to access it anywhere.
Okay I have made NPC Plyr[100] global by placing it in the cpp file (which I cannot believe I overlooked to do) but the function in the header file tells me:
Error 1 error C2065: 'Plyr' : undeclared identifier c:\documents and settings\visual studio 2008\projects\classexperiments\classexperiments\globals.h 182 ClassExperiments
I think I need to use the extern keyword to make NPC Plyr[100] available to the header file - but I'm uncertain of the syntax.
**EDIT - Actually I think I have it working now - thanks to you all for your time and patience!