I'm writing some code that reads in a number of lines and builds objects based on them, that are added to a vector. I know the format of the file, so the code is very straight forward. The objects are pure POD structs and the constructor just takes the read values and assigns them to the members.
Sample code:
1 2 3 4 5 6 7 8 9 10
struct Item {
double A, B, C, D;
Item(double pA, double pB, double pC, double pD) : A(pA), B(pB), C(pC), D(pD) {}
}
// Somewhere in a reader function, in a "per line" loop, with Items is a std::vector<Item>:
double A, B, C, D;
currentLine >> A >> B >> C >> D;
Items.push_back(Item(A, B, C, D));
And this doesn't work. I can split up the last line to make it work:
1 2
Item P(A, B, C, D);
Items.push_back(P);
The thing that fails is that for some reason, the 'C' member variable of each Item would not have a value (well, '0.0', which probably means uninitialized as this is MSVC in Debug mode). All the others would be fine, just the 'C' wouldn't be set. I'm assuming this is a ooincidence and it's not about 'C', it's just something that fucks up entirely.
So, my question is: why doesn't that work? To my knowledge, vector's push_back makes a basic copy. I'm not really clear on the conceptual differences between the two syntaxes, but I can't think of a reason why the one-line version doesn't work.
I don't see any obvious problems with the code you have posted. Could you post some real code that compiles with the error you get or at least post the error message you get.
There's not much relevant code to add. It's literally "extract numbers from stringstream, build object with those numbers as parameters and push it into a vector". There is no error; just one of member variables of the object always being 0.0 for every object in the vector, unless I split up the construction and push_back into separate steps.
I'll try to extract that piece of code from the project to reproduce it.
I'm extra confused now, because after it wouldn't bug out on my sample code, I went back to the original code and it works perfectly both ways now. Guess I'll just accept this as a win in the long run...
Thanks for the effort though. At least now I know that there aren't any behind-the-scene oddities in vector's push_back. Just my own stupidities.
It could be some kind of undefined behaviour going on. It can be caused by doing something illegal like dereferencing a dangling pointer, accessing an array or vector out of bounds, etc. These things can have subtle (and not so subtle) side effects that are not always easy to spot.
That's the odd thing, there was really absolutely nothing going on except "read data, push into vector, read from vector". No pointers, nothing. The 0's showed up both at the end (when data is printed out), in the calculations (the bugged value was multiplied and summed to a total) and in the debugger. I checked the state of the vector at several points. I witnessed the values being extracted from the stringstream correctly, and then an object with one '0' value and 3 correct values being in the vector.
I'm wildly confused. Should I encounter it again, I'll continue this post. Until then, guess it is "solved".