samedi 26 décembre 2015

std::vector push_back() erases previous element

I have a class "terrain" (every single special function is defined - I'm desperate)

So, when i define objects one by one, everything works fine, but it's ineffective when it comes to multiple objects. So i made a std::vector of my terrain.

std::vector<terrain> defined_terrains;

Everything looks fine, I have a function that prints all content of objects. So I do:

defined_terrains.push_back({args...})
defined_terrains[0].print_data();

The above code works like this:

  /// push_back starts 
COPY CSTR: object test 1
DSTR: object test 1
  /// push_back finishes

  /// print_data starts
Data:
name: object test 1
id: 1003
hitbox:
{
-0.0520833
-0.078125

-0.0520833
0.078125

0.0520833
0.078125

0.0520833
-0.078125

}

frames:
{
-0.0520833
-0.078125

-0.0520833
0.078125

0.0520833
0.078125

0.0520833
-0.078125

}

RGB:
{
0.392157
0.784314
0.196078

}
offset:
x: 0.104167
y: -0.3125
on ground: no
fall time: 0
direction 1
destructible: 0
transparent: 0
grounding:
 left limit: 0
 right limit: 0
blocked from above: 0
  /// print_data finishes

Everything is perfect. But then i add next element to std::vector:

defined_terrains.push_back({args...})
defined_terrains[1].print_data();

This happens:

*everything the same for object test 1*

  /// push_back starts 
COPY CSTR: object test 2
COPY CSTR: object test 1  <----- why does this happen? defined_terrains[0] was not involved here
DSTR: object test 1 <----------- its like defined_terrains[1] used defined_terrains[0] like temporary (???)
DSTR: object test 2
  /// push_back finishes 

  /// print_data starts
name: object test 2
id: 1004
hitbox:
{
-0.0520833
-0.078125

-0.0520833
0.078125

0.0520833
0.078125

0.0520833
-0.078125

}

frames:
{
-0.0520833
-0.078125

-0.0520833
0.078125

0.0520833
0.078125

0.0520833
-0.078125

}

RGB:
{
0.392157
0.784314
0.196078

}
offset:
x: -0.104167
y: -0.390625
on ground: no
fall time: 0
direction 1
destructible: 0
transparent: 0
grounding:
 left limit: 0
 right limit: 0
blocked from above: 0
  /// print_data finishes

Am I stupid or something? Or is there a serious bug with push_back()?

Aucun commentaire:

Enregistrer un commentaire