For some reason this isn’t working for me. It gives me the vector iterator out of range error.
directory_entry TDE("Path");
vector <directory_entry> Temp;
Temp.push_back(TDE);
User_Data->DPath.insert(User_Data->DPath.begin(), Temp.begin(), Temp.end());
But, this works,
vector <directory_entry> DPath;
directory_entry TDE("Path");
vector <directory_entry> Temp;
Temp.push_back(TDE);
DPath.insert(DPath.begin(), Temp.begin(), Temp.end());
I don’t think there is anything wrong with User_Data->DPath because I can push/pop and access elements in it. But for some reason I can’t seam to be able to use insert on it without getting out of range errors.
Does anyone know why this might be?
edit: A popup emerges, debug assertion failed. It gives me a line in the vector header file, 1111, and a message “Expression: vector iterator out of range”. If I make sure that there is at least one element in User_Data->DPath, and then start at .begin+1, I get “Expression: vector iterator+offset out of range” and it gives me line 157 of the vector header file.
edit: You are all probably right. The g_new0 function does the memory allocation http://developer.gnome.org/glib/2.32/glib-Memory-Allocation.html#g-new0
struct_type : the type of the elements to allocate. n_structs : the
number of elements to allocate. Returns : a pointer to the allocated
memory, cast to a pointer to struct_type.
typedef struct {
vector <directory_entry> DPath;
}State;
static gboolian select_dir (ClutterActor *actor, ClutterEvent *event, g_pointer data){
State *User_Data = (State*)data;
directory_entry Temp(Path);
User_Data->DPath.push_back(Temp);
...
return TRUE;
}
int main( argc, char*argv[]){
State *data = g_new0 (State, 1);
...
g_signal_connect(Cluter_Actor, "button-event", G_CALLBACK(select_dir), data)
...
clutter_main();
g_free(data);
return 0;
}
g_new0is not a drop-in replacement fornewnewdoes two things: allocates memory for an object, and calls the object’s constructor.g_new0only does the first, allocating memory. You need to call the object’s constructor explicitly if you want to useg_new0. This is done using “placement new”:The reason calling
State‘s constructor is important is that it in turn calls the constructor of thevector<directory_entry>member of State, and this is what initializes the vector. Without initializing the vector properly, you cannot use it.Note that since you are calling the constructor explicitly, you will also need to call the destructor explicitly before freeing the memory:
Is there a reason you are using
g_new0instead of justnew?