I have this code. scores
is a field in HiscoreTable
of type std::vector<reference_wrapper<Record>>
void HiscoreTable::replaceRecord(Record rec)
{
bool found = false;
for (reference_wrapper<Record> r : scores) {
if (r.get().name == rec.name) {
r = rec;
found = true;
break;
}
}
if (!found) {
scores.push_back(rec);
}
}
And if I try to do for(reference_wrapper<Record> r : scores) cout << r.get() << endl;
, a bad_alloc is thrown. Is it because r
is not a Record&
but a reference_wrapper<Record>
and therefore I cannot assign a Record
to it? I thought reference_wrapper<Record>::operator=
was overloaded to allow such a thing?
EDIT: Turns out it was a problem in my destructor, where the printing loop was located. At some point, I passed a HiscoreTable in to a helper function by value, and when it got destructed at the end of the helper function, it invalidated the callsite's HiscoreTable. I don't know why printing would break a reference, but I guess that's what happened.