Fair warning this is more code than I like putting on here, but I didn't want to miss anything, if you skip to main, you'll see most of it isn't doing much. I read this vector.size() returns 1 even though there are more than 1 elements in it? but it didn't help, haven't seen anything else like my problem.
I've been troubleshooting an error where my vector(s) are filling up with data that doens't belong to them. It doesn't seem to be garbage, in fact, its numbers that my other data structures contain.
It may have something to do with how my header files have been linked together(I have never joined them this way before), that said, I've traced it back far enough to no longer have the ability to troubleshoot it myself.
I have simplified my program to the point of just displaying the error, while including its base structure.
The code...
Parent:
Pile.H
#ifndef PILE_H
#define PILE_H
#include <iostream>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <algorithm>
// contains rand( ) and srand( ) for random number generation
#include <cstdlib>
// contains definitions of time functions
#include <chrono>
#include <random> // std::default_random_engine
#include <stack>
#include <iomanip>
using namespace std;
class Pile
{
protected:
vector<int> cards;
public:
void createDeck();
vector<int> getCards();
};
#endif
Pile.cpp
#include "Pile.h"
/*=Pile
******************/
// Creates 52 shuffled cards, 1-13 == "♠️", 14-26 == "♣️", 27-39 == "♦️", 40-52 == "♥️"
void Pile::createDeck()
{
for(int i = 1;i <= 52; i++)
cards.push_back(i);
// shuffleDeck();
}
vector<int> Pile::getCards()
{
return cards;
}
Children:
DrawPile.h
#ifndef DRAWPILE_H
#define DRAWPILE_H
#include "Pile.h"
class DrawPile : public Pile
{
protected:
queue<int> cards;
public:
DrawPile( vector<int> visitingCards );
// // getters
queue<int> getCards();
};
#endif
DrawPile.cpp
#include "DrawPile.h"
DrawPile::DrawPile(vector<int> visitingCards)
{
for(int i = 0;i < 24; i++)
cards.push( visitingCards[i] );
}
Hand.h
#ifndef HAND_H
#define HAND_H
#include "Pile.h"
class Hand : public Pile
{
protected:
deque<int> hand1;
deque<int> hand2;
deque<int> hand3;
deque<int> hand4;
deque<int> hand5;
deque<int> hand6;
deque<int> hand7;
public:
void fillHand(vector<int> visitingCards);
// getters
deque<int> getHand1();
deque<int> getHand2();
deque<int> getHand3();
deque<int> getHand4();
deque<int> getHand5();
deque<int> getHand6();
deque<int> getHand7();
// int getFace(int number);
vector< deque<int> > getHands();
};
#endif
Hand.cpp
#include "Hand.h"
void Hand::fillHand(vector<int> visitingCards)
{
// refactor
// hand1{24}, hand2{25,26}, hand3{27, 28, 29}, hand4{30, 31, 32, 33},
// hand5{34, 35, 36, 37, 38}, hand6{39, 40, 41, 42, 43, 44},
// hand7{45, 46, 47, 48, 49, 50, 51}
hand1.push_back( visitingCards[24] );
hand2.push_back( visitingCards[25] );
hand2.push_back( visitingCards[26] );
hand3.push_back( visitingCards[27] );
hand3.push_back( visitingCards[28] );
hand3.push_back( visitingCards[29] );
hand4.push_back( visitingCards[30] );
hand4.push_back( visitingCards[31] );
hand4.push_back( visitingCards[32] );
hand4.push_back( visitingCards[33] );
hand5.push_back( visitingCards[34] );
hand5.push_back( visitingCards[35] );
hand5.push_back( visitingCards[36] );
hand5.push_back( visitingCards[37] );
hand5.push_back( visitingCards[38] );
hand6.push_back( visitingCards[39] );
hand6.push_back( visitingCards[40] );
hand6.push_back( visitingCards[41] );
hand6.push_back( visitingCards[42] );
hand6.push_back( visitingCards[43] );
hand6.push_back( visitingCards[44] );
hand7.push_back( visitingCards[45] );
hand7.push_back( visitingCards[46] );
hand7.push_back( visitingCards[47] );
hand7.push_back( visitingCards[48] );
hand7.push_back( visitingCards[49] );
hand7.push_back( visitingCards[50] );
hand7.push_back( visitingCards[51] );
}
deque<int> Hand::getHand1()
{
return hand1;
}
deque<int> Hand::getHand2()
{
return hand2;
}
deque<int> Hand::getHand3()
{
return hand3;
}
deque<int> Hand::getHand4()
{
return hand4;
}
deque<int> Hand::getHand5()
{
return hand5;
}
deque<int> Hand::getHand6()
{
return hand6;
}
deque<int> Hand::getHand7()
{
return hand7;
}
TargetPile.h
#ifndef TARGETPILE_H
#define TARGETPILE_H
#include "Pile.h"
class TargetPile : public Pile
{
protected:
int cards;
public:
int getCards();
};
#endif
TarpetPile.cpp
#include "TargetPile.h"
int TargetPile::getCards()
{
return cards;
}
Main Driver.cpp
#include "Pile.h"
#include "DrawPile.h"
#include "Hand.h"
#include "TargetPile.h"
int main()
{
cout << endl;
// main loop control variable
int selection;
// Initial game setup.
Pile solitaire;
solitaire.createDeck();
DrawPile drawPile( solitaire.getCards() );
Hand hand;
hand.fillHand( solitaire.getCards() );
TargetPile targetPile;
// Welcome message
cout << "\nWelcome to Solitaire!";
// I want to loop back until the user selection is equal to 5
do{
cout << "hand.getHand1().size(): " << hand.getHand1().size();
for (int i = 0; i < 15; i++) {
cout << "\ngetHand1()[i]: " << hand.getHand1()[i];
}
cout << endl << endl;
cout << "hand.getHand2().size(): " << hand.getHand2().size();
for (int i = 0; i < 15; i++) {
cout << "\ngetHand2()[i]: " << hand.getHand2()[i];
}
cout << "\nget selection (4 to end)";
cin >> selection;
}while(selection != 4);
// Exit Message
cout << "\nThanks for playing!";
cout << endl;
} // End main
compile method
g++ -std=c++11 -c *.cpp
g++ -std=c++11 *.o -o a.out
./a.out
The error doesn't appear immediately, sometimes it takes 5 passes in the "do while" loop to come up, but it will.
as of right now, hand1 seems to get an extra element appended to it, 27, matching the second element of hand2
hand.getHand1().size(): 1
hand1[0] = 25 // correct
hand1[0] = 27 // incorrect
In my full program it will pull in, even more, data, hand1 will look like this
hand.getHand1().size(): 1
hand1[0] = 25 // correct
hand1[0] = 1 // incorrect
hand1[0] = 2 // incorrect
hand1[0] = 3 // incorrect
up to 10
How is it possible for a vectors size to have a value of 1 and yet it still has more elements in it and how do I prevent the unwanted (and uncalculated) data from popping into my vector?
Aucun commentaire:
Enregistrer un commentaire