English 中文(简体)
矢量插入 () 错误: 矢量迭代器除以范围
原标题:vector insert() error: vector iterator offset out of range
  • 时间:2012-05-28 06:55:34
  •  标签:
  • c++
  • vector

出于某种原因,这对我没用。 它给了我矢量传动器, 超出了范围错误 。

directory_entry TDE("Path");
vector <directory_entry> Temp;
Temp.push_back(TDE);

User_Data->DPath.insert(User_Data->DPath.begin(), Temp.begin(), Temp.end());

但是,这工作,

vector <directory_entry> DPath;
directory_entry TDE("Path");
vector <directory_entry> Temp;
Temp.push_back(TDE);

DPath.insert(DPath.begin(), Temp.begin(), Temp.end());

我不认为 Use_ Data- gt; DPath 有任何问题, 因为我可以推/ pop 和访问其中的元素 。 但出于某种原因, 我无法在不超出范围错误的情况下使用插入 。

有人知道为什么吗?

< strong > 编辑 : 弹出一个弹出, 调试声明失败。 它给了我矢量页眉文件中的一行, 1111, 和一条消息“ 表达: 矢量代用器超出范围 ” 。 如果我确定在用户_ Data- gt; DPath 中至少有一个元素, 然后从. begin+1 开始, 我得到“ 表达: 矢量矢量 Iterator+offet 出范围”, 它给了我矢量页眉文件中的第157行 。

edit: you are all probably right.org/g_new0 函数使记忆分配http://developer.gnome.org/glib/2.32/glib-Memory-Alplace.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_new0 不是一个 new 的投放替换器

new 做两件事: 分配对象的内存, 并调用对象的构造器 。 g_ new0 只使用第一个, 分配内存。 如果您想要使用 g_new0 , 您需要明确调用对象的构造器。 这是使用“ 替换新” 完成的 :

State *data = g_new0 (State, 1); 
new (data) State;  // placement new - calls the constructor

调用 State s constructionor is 很重要的原因在于它反过来又调用 victor< directory_entry> 成员国成员的构建者,这就是初始化矢量的原因。如果不正确初始化矢量,您就无法使用它 。

请注意,既然你正在明确调用建造者,你还需要在释放内存之前明确调用毁灭者:

data->~State();  // call destructor
g_free(data);    // free the memory

您之所以使用 g_new0 而不只使用 new 的原因吗?

State *data = new State;
...   // use data
delete data;
问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签