9
I have a problem while working with vectors of a class. I have tried whittling away at the code as much as possible, leaving me with the code below. Yet, after doing this, I still cannot figure out why my program is crashing. I come from a world of VBA and my C++ debugging skills are poor to say the least and I apologize for that up front. Any guidance to improve my ability here will be gladly accepted.
My class:
class Tester {
public:
int varA;
int varB;
Tester() {
varA = 1;
varB = 1;
}
Tester(Tester& P1, Tester& P2) {
varA = P1.varA + P2.varA;
varB = P1.varB + P2.varB;
}
Tester(const Tester &Source) {
varA = Source.varA;
varB = Source.varB;
}
};
My test mains:
Does not work:
int main() {
std::vector < Tester > BreakIt;
for (int i = 0; i < 2500; i++) {
Tester newTester;
BreakIt.push_back(newTester);
Tester& tempTester = BreakIt.at(0);
for (int j = 0; j < 4; j++) {
BreakIt.push_back(Tester(newTester, tempTester)); //This is where it crashes.
}
}
return 0;
}
Does work:
int main() {
std::vector < Tester > BreakIt;
Tester newTester;
BreakIt.push_back(newTester);
for (int i = 0; i < 2500; i++) {
Tester& tempTester = BreakIt.at(0);
for (int j = 0; j < 4; j++) {
BreakIt.push_back(Tester(newTester, tempTester));
}
}
return 0;
}
Does work:
int main() {
std::vector < Tester > BreakIt;
for (int i = 0; i < 2500; i++) {
Tester newTester;
BreakIt.push_back(newTester);
Tester& tempTester = BreakIt.at(0);
}
return 0;
}
The push_back() line that breaks does run a few times before t