开发者

Help with constructor (pusing_back elements into a pointer to an array)

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 se开发者_开发百科ems 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.


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:

vector<Recipe> recipes_; //member

Recipe defaultRec;
recipes_.push_back(defaultRec);

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜