Ok I feel dumb
I’m sorry if I am posting this in the wrong section but this I thought bust suited my question. So here it goes. I’m getting an error on this piece of code that I just don’t understand.
#include <vector>
std::vector<tChapter> g_Chapter;
int g_iCurrChapter = 0;
int main (){
tChapter firstChapter = {0};
g_Chapter.push_back(firstChapter);
g_Chapter[g_iCurrChapter].NumObjs = 0; //<- what’s wrong
}
It compiles and runs but once it executes the line “g_Chapter[g_iCurrChapter].NumObjs = 0;” is causes a break and for the life of me I cant seem to figure it out.
What does your tChapter class/struct look like?
You only have a few points of failure there:
* g_iCurrChapter is somehow larger than your vector. Perhaps you are ending up with a value other than 0 in it? Perhaps you are running out of memory, and the push_back is failing? Perhaps an exception is getting thrown?
* Perhaps NumObjs is a complex object that has problems with assignment to 0?
A side note, but what are you trying to do with your tChapter, when you initialize it to 0?
You only have a few points of failure there:
* g_iCurrChapter is somehow larger than your vector. Perhaps you are ending up with a value other than 0 in it? Perhaps you are running out of memory, and the push_back is failing? Perhaps an exception is getting thrown?
* Perhaps NumObjs is a complex object that has problems with assignment to 0?
A side note, but what are you trying to do with your tChapter, when you initialize it to 0?
Post the tChapter class if you can. The only way I can see that this would be caused is if g_iCurrChapter is modified elsewhere before that line occurs (eg. in the tChapter constructor). This is under the assumption that NumObjs's type is just a built-in (like int) and that your system isn't so overloaded that your running out of memory.
I’m sorry I can’t give you all of my code but I can give you a sample of what I am trying to do. Long story short this is part of my game tools. This is an over glorified map editor. Each level is stored as a chapter and there are X number of objects in the chapter. As shown below
class tPdxObjs{
public:
int ObjType; // Object type
int ObjRef; // refrence Number for object
char strObjName[50]; // a string name of this object
char strFileName[50]; // the file name if there is one
tMaterialInfo Material; //<-- just basic struct
int customTypeIndex;
unsigned int m_texture;
void Init()
{
m_texture = -1;
vScale.m_x = 1;
vScale.m_y = 1;
vScale.m_z = 1;
sprintf(Material.strFile,"%s","Unused");
Material.fColor[0] = 0.5f;
Material.fColor[1] = 0.5f;
Material.fColor[2] = 0.5f;
selected = FALSE;
}
};
class tChapter{
public:
std::vector<tPdxObjs> pdxObj;
int NumObjs;
};
// in my game lib
int g_iCurrChapter = 0;
std::vector<tChapter> g_LuaChapter;
// in the actual app
std::vector<tChapter> g_Chapter;
Now my problem happens not when I’m using g_LuaChapter or g_Chapter the problem happens when I’m trying to get one to be the same as the other such as
swap ( g_Chapter[g_iCurrChapter].pdxObj, g_LuaChapter[g_iCurrChapter].pdxObj); the g_LuaChapter is comming from a dll and g_Chapter is in an MFC applacation the error givein is second assertion failed or subscript out of range. Any help would be aprishated ty.
class tPdxObjs{
public:
int ObjType; // Object type
int ObjRef; // refrence Number for object
char strObjName[50]; // a string name of this object
char strFileName[50]; // the file name if there is one
tMaterialInfo Material; //<-- just basic struct
int customTypeIndex;
unsigned int m_texture;
void Init()
{
m_texture = -1;
vScale.m_x = 1;
vScale.m_y = 1;
vScale.m_z = 1;
sprintf(Material.strFile,"%s","Unused");
Material.fColor[0] = 0.5f;
Material.fColor[1] = 0.5f;
Material.fColor[2] = 0.5f;
selected = FALSE;
}
};
class tChapter{
public:
std::vector<tPdxObjs> pdxObj;
int NumObjs;
};
// in my game lib
int g_iCurrChapter = 0;
std::vector<tChapter> g_LuaChapter;
// in the actual app
std::vector<tChapter> g_Chapter;
Now my problem happens not when I’m using g_LuaChapter or g_Chapter the problem happens when I’m trying to get one to be the same as the other such as
swap ( g_Chapter[g_iCurrChapter].pdxObj, g_LuaChapter[g_iCurrChapter].pdxObj); the g_LuaChapter is comming from a dll and g_Chapter is in an MFC applacation the error givein is second assertion failed or subscript out of range. Any help would be aprishated ty.
Different STL libraies? If the lib is built with using an older version or different version of the STL libary than the app, you can't exchange their data with a swap like that, it would proablly result in some memory overrunn. The only sure way to build with the same STL libraries is to explictly set the stl paths yourself for both the app and the lib.
Good Luck!
-ddn
Good Luck!
-ddn
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement