As an assignment I have to create "CMD Battleship"
Not my favorite project but basically I have in someway create a 5x5 board (4x4 for the player) and let the players place and name their ships etc. Then they do the old fashioned guess where your opponents ships are. The plan I've had in mind is to create 5 individual rows. (No MD arrays/vectors!) With what I've had in mind I'll need to pass these rows around a bit. That can be a bit of pain with arrays (not impossible but a pain) so I thought it better to use vectors, and I would just be careful to make sure the size stays what it needs to be etc. (From my understanding you can't give a vector a fixed size without doing some of your own handiwork)
These vectors are inside my Board class, which has a constructor. Here's the constructor.
Board::Board(){
Board::boardRow_1.reserve(6);
Board::boardRow_2.reserve(6);
Board::boardRow_3.reserve(6);
Board::boardRow_4.reserve(6);
Board::boardRow_5.reserve(6);
for(int i = 0; i <= 4; i++){
Board::boardRow_1.push_back(' ');
}
for(int i = 0; i <= 4;){
Board::boardRow_2.push_back(' ');
}
for(int i = 0; i < 4; i++){
Board::boardRow_3.push_back(' ');
}
for(int i = 0; i <= 4; i++){
Board::boardRow_4.push_back(' ');
}
for(int i = 0; i <= 4; i++){
Board::boardRow_5.push_back(' ');
}
Board::boardRow_1[1] = '1';
Board::boardRow_1[2] = '2';
Board::boardRow_1[3] = '3';
Board::boardRow_1[4] = '4';
Board::boardRow_2[0] = '1';
Board::boardRow_3[0] = '2';
Board::boardRow_4[0] = '3';
Board::boardRow_5[0] = '4';
}
I originally wasn't calling reserve for each vector but I ran into bad_alloc and thought maybe I could try reserving 6 elements for each vector (extra to be safe) and I'm still getting std::bad_alloc instance. I'm positive it's coming from the class constructor.
Lol I guess that's what you get when you code while you're tired. I'll be that's the problem. I guess it's trying to add elements to boardRow_2 forever lol.