R
RonHiler
My copy constructor is crashing my program, and I can't figure out
why. I'll try to make the code listing as short as I can. Here are
the two headers:
class BreakthroughClass
{
public:
BreakthroughClass();
virtual ~BreakthroughClass();
BreakthroughClass(const BreakthroughClass &source);
BreakthroughClass& operator=(const BreakthroughClass &source);
protected:
char Name[256];
char Description[1024];
int LocationX;
int LocationY;
vector <unsigned int> Prereqs;
friend class TechManagerClass;
};
class TechManagerClass
{
public:
TechManagerClass();
virtual ~TechManagerClass();
void InitializeTechs();
private:
vector <BreakthroughClass> Breakthroughs;
void BreakthroughLoadTitles(char *RCLabel);
void BreakthroughLoadData(char *RCLabel);
//single instance, do not implement these functions
TechManagerClass(const TechManagerClass &source);
TechManagerClass& operator=(const TechManagerClass &source);
};
TechManagerClass TechManager;
So Breakthroughs are a vector of BreakthroughClass contained in the
TechManagerClass, and TechManager is a global instance of
TechManagerClass.
When I call TechManager.InitializeTechs, I do this:
void TechManagerClass::InitializeTechs()
{
BreakthroughLoadTitles("SCIENCE");
BreakthroughLoadTitles("GOVERNMENT");
//and so on
}
Then:
void TechManagerClass::BreakthroughLoadTitles(char *RCLabel)
{
int i;
char FullLabel[256];
HRSRC ResourceLocation;
HANDLE Resource;
LPVOID ResourcePointer;
char* pChar; //temporary tranversing variables
BreakthroughClass NewBreakthrough;
char OutputString[1024];
i = 0;
while (1)
{
sprintf (FullLabel, "%s_%d", RCLabel, i);
sprintf(OutputString, "Loading %s Technology Breakthrough",
FullLabel);
OutputDebugString(OutputString);
ResourceLocation = FindResourceEx (NULL, RT_RCDATA, FullLabel,
MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL));
if (ResourceLocation == NULL)
break;
Resource = LoadResource (NULL, ResourceLocation);
ResourcePointer = LockResource(Resource);
pChar = (char*)ResourcePointer;
strcpy(NewBreakthrough.Name, pChar);
OutputDebugString(NewBreakthrough.Name);
Breakthroughs.push_back(NewBreakthrough);
i++;
}
sprintf(OutputString, "Loaded %i %s Technology Breakthroughs", i,
RCLabel);
OutputDebugString(OutputString);
}
So I'm finding the entry in my resource file (which are named
SCIENCE_0, SCIENCE_1, GOVERNMENT_0, GOVERNMENT_1, and so on), copying
the name into my temporary NewBreakthrough variable, then doing a
push_back into Breakthroughs. This calls my copy constructor, and
here's where I have problems. I took everything out (for now) except
the one line, and this is the line causing the crash.
BreakthroughClass::BreakthroughClass(const BreakthroughClass &source)
{
strcpy (Name, source.Name);
}
I've done OutputDebugString on the Name entries right into the copy
constructor, and it seems to be coming out fine (I get the name of the
techs), so I don't see why the strcpy is crashing. What am I doing
wrong?
Thanks,
Ron
why. I'll try to make the code listing as short as I can. Here are
the two headers:
class BreakthroughClass
{
public:
BreakthroughClass();
virtual ~BreakthroughClass();
BreakthroughClass(const BreakthroughClass &source);
BreakthroughClass& operator=(const BreakthroughClass &source);
protected:
char Name[256];
char Description[1024];
int LocationX;
int LocationY;
vector <unsigned int> Prereqs;
friend class TechManagerClass;
};
class TechManagerClass
{
public:
TechManagerClass();
virtual ~TechManagerClass();
void InitializeTechs();
private:
vector <BreakthroughClass> Breakthroughs;
void BreakthroughLoadTitles(char *RCLabel);
void BreakthroughLoadData(char *RCLabel);
//single instance, do not implement these functions
TechManagerClass(const TechManagerClass &source);
TechManagerClass& operator=(const TechManagerClass &source);
};
TechManagerClass TechManager;
So Breakthroughs are a vector of BreakthroughClass contained in the
TechManagerClass, and TechManager is a global instance of
TechManagerClass.
When I call TechManager.InitializeTechs, I do this:
void TechManagerClass::InitializeTechs()
{
BreakthroughLoadTitles("SCIENCE");
BreakthroughLoadTitles("GOVERNMENT");
//and so on
}
Then:
void TechManagerClass::BreakthroughLoadTitles(char *RCLabel)
{
int i;
char FullLabel[256];
HRSRC ResourceLocation;
HANDLE Resource;
LPVOID ResourcePointer;
char* pChar; //temporary tranversing variables
BreakthroughClass NewBreakthrough;
char OutputString[1024];
i = 0;
while (1)
{
sprintf (FullLabel, "%s_%d", RCLabel, i);
sprintf(OutputString, "Loading %s Technology Breakthrough",
FullLabel);
OutputDebugString(OutputString);
ResourceLocation = FindResourceEx (NULL, RT_RCDATA, FullLabel,
MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL));
if (ResourceLocation == NULL)
break;
Resource = LoadResource (NULL, ResourceLocation);
ResourcePointer = LockResource(Resource);
pChar = (char*)ResourcePointer;
strcpy(NewBreakthrough.Name, pChar);
OutputDebugString(NewBreakthrough.Name);
Breakthroughs.push_back(NewBreakthrough);
i++;
}
sprintf(OutputString, "Loaded %i %s Technology Breakthroughs", i,
RCLabel);
OutputDebugString(OutputString);
}
So I'm finding the entry in my resource file (which are named
SCIENCE_0, SCIENCE_1, GOVERNMENT_0, GOVERNMENT_1, and so on), copying
the name into my temporary NewBreakthrough variable, then doing a
push_back into Breakthroughs. This calls my copy constructor, and
here's where I have problems. I took everything out (for now) except
the one line, and this is the line causing the crash.
BreakthroughClass::BreakthroughClass(const BreakthroughClass &source)
{
strcpy (Name, source.Name);
}
I've done OutputDebugString on the Name entries right into the copy
constructor, and it seems to be coming out fine (I get the name of the
techs), so I don't see why the strcpy is crashing. What am I doing
wrong?
Thanks,
Ron