Instance variables seem to be inaccessible.

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.
Apologies..

The CPP file has this:

GenerateNPCs(2);

And the included header file has:

class NPC
{
public:
int vision;
}


and (in brief)

int GenerateNPCs(int number_of_NPCs_to_create)
{
NPC Plyr[100];
...
Plyr[x].vision = iRnd(60);
...
Return 0;
}


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.
Last edited on
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.

Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <vector>
using namespace std;

class NPC
{
   public:
     NPC() {}
     NPC(int vision) : vision(vision) {}
     int vision;
};

vector<NPC> GenerateNPCs(int number_of_NPCs_to_create)
{
  vector<NPC> npcs;
  for (int i=0;i<number_of_NPCs_to_create;i++)npcs.push_back(NPC(iRnd(60)));
  return npcs;
}

int main()
{
  vector<NPC> npcs=GenerateNPCs(2);
  cout << npcs[0].vision << endl;
}
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.

Thanks again!
you could also declare NPC Plyr[100]; globally and then use int GenerateNPCs(int number_of_NPCs_to_create) to assign it its values.
I wasn't able to get either of these suggestions to work for me, does the vector solution mean the objects remain alive?

How could I implement wtf's suggestion?

Been scanning the net some more and wondered also if creating a thread to deal with the objects would keep them alive - and would they be accessible?

I'm sorry to ask what might be dumb questions - I'd just like to have a clearer understanding.
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!
Last edited on
Use extern in any file that needs access to Plyr. It's basically like a prototype for the variable, in a way.
Topic archived. No new replies allowed.