帮助构造函数(将_back元素放入指向数组的指针中)
我在将元素插入指向我在代码中定义的某些元素(在本例中为食谱)的向量的指针时遇到问题。在代码的其他部分中,使用 Push_back 似乎工作正常,但如果我在此代码中使用它:
{
Recipe defaultRec;
this->recipes_ = new vector<Recipe>;
this->recipes_->push_back(defaultRec);
}
我收到以下错误消息:
“Assignment 2.exe 中 0x01164031 处未处理的异常:0xC0000005:访问冲突读取位置 0xccccccce0”
Recipes_的声明是:
vector<Recipe>* recipes_;
希望任何人都可以帮助我,提前致谢。
I have a problem inserting elements into a pointer to a vector of some elements I defined in my code (in this case Recipes). In some other parts of the code, using push_back seems to work fine, but if I use it in this code:
{
Recipe defaultRec;
this->recipes_ = new vector<Recipe>;
this->recipes_->push_back(defaultRec);
}
I get the following error message:
"Unhandled exception at 0x01164031 in Assignment 2.exe: 0xC0000005: Access violation reading location 0xcccccce0"
The declaration of recipes_ is:
vector<Recipe>* recipes_;
Hope anyone can help me, thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的代码对我来说看起来不错。我确信问题出在其他地方。
顺便问一下,为什么使用指向向量的指针?为什么不这样做:
至少这可以让您免于分配和释放。此外,很可能,指向向量的指针并不比上面的更好!
Your code looks fine to me. I'm sure the problem is somewhere else.
By the way, why do you use pointer to vector? Why not this:
Atleast this saves you from allocation and deallocation. Besides, most likely, pointer to vector doesn't serve you any better than the above!
你的代码没有任何问题。问题可能出在其他地方。此类问题通常是由内存过度写入引起的,例如写入超出数组末尾或写入未初始化的内存位置。
There is nothing wrong with your code. The problem likely lies elsewhere. Problems like this are often caused by memory over writing such as writing past the end of an array or writing to an uninitialised memory location.