c++ - bad access error when pushing back vector -
i have these 2 classes:
class card { //class represents card int cardnumber; //the id of card give itse level , suit std::vector<std::string> suits = { "hearts", "spades", "diamonds", "clubs" }; std::vector<std::string> levels { "ace", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "jack", "queen", "king"}; std::string suit; std::string level; public: card(int ncardnumber) { //constructor initiliazes properties of card cardnumber = ncardnumber; level = levels[cardnumber%13]; suit = suits[cardnumber/13]; } void printvalue() { //prints cards level , suit using namespace std; cout << level << " of " << suit << endl; } };
and
class hand { //represents hand player holding int playernumber; std::vector<card> playerhand; //vector of carsa public: hand(int m_playernumber) { playernumber = m_playernumber; } void addcard(int cardnumber) { //adds card hand card cardtobeadded(cardnumber); cardtobeadded.printvalue(); playerhand.push_back(cardtobeadded); } void printhand() { using namespace std; cout << "player " << playernumber << " has:" << endl; (int = 0; < 2; i++) { playerhand[i].printvalue(); } } };
and whenever call addcard
function in hand
, exc_bad_access (code=1).
i've tried debugging declaring 2 members in playerhandvector
, changing function rewrite values opposed appending new ones vector, same error.
any appreciated!
with proper debugging, it's pretty easy see what's going on. i've reduced example minimum reproduce bug, because don't believe it's std::vector::push_back
.
#include <iostream> #include <vector> #include <string> class card { //class represents card int cardnumber; //the id of card give itse level , suit std::vector<std::string> suits = { "hearts", "spades", "diamonds", "clubs" }; std::vector<std::string> levels { "ace", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "jack", "queen", "king"}; std::string suit; std::string level; public: card(int ncardnumber) { //constructor initiliazes properties of card cardnumber = ncardnumber; std::cout << cardnumber % 13 << std::endl; std::cout << cardnumber / 13 << std::endl; level = levels[cardnumber%13]; suit = suits[cardnumber/13]; } void printvalue() { //prints cards level , suit using namespace std; cout << level << " of " << suit << endl; } }; int main() { std::vector<card> v; (size_t n = 0; n <= 52; ++n) { // replicate "addcard" function card c(n); c.printvalue(); v.push_back(c); } return 0; }
you'll notice 2 things here: i've added debugging card
constructor because believed problem access of std::vector<std::string>
, i'm attempting add 52 cards. 'lo , behold, first 51 cards add successfully; however, trying add king of clubs has error. let's see why:
card(int ncardnumber) { // ncardnumber = 52; cardnumber = ncardnumber; // 52 const int ncard = cardnumber % 13; // king const int nsuit = cardnumber / 13; // 4 level = levels[ncard]; suit = suits[nsuit]; // whoops, out of bounds! }
you can see here trying access suits[4]
out of bounds.
Comments
Post a Comment