Note that there are some explanatory texts on larger screens.

plurals
  1. POSort vector of object
    text
    copied!<p>I'm in stuck with one issue. Here's my code. I have <code>class Card</code> and <code>class deckOfCards</code>. My problem is in functions <code>straight</code> and <code>sortFunc</code>. I want to sort my vector that I created in <code>main</code> called <code>fiveHand</code>. I saw a lot of examples but in my case I can't understand how to do this correctly. </p> <p>I have errors like <code>i</code> and <code>j</code> were not declared:</p> <pre><code>#include &lt;iostream&gt; using namespace std; class Card { private: int m_suit; int m_face; public: Card(int face, int suit); static string suits[]; static string faces[]; static string toString(string s_face, string s_suit); int getFace(); void setFace(int face); int getSuit(); void setSuit(int suit); }; Card::Card(int face, int suit) { m_face = face; m_suit = suit; } string Card::faces[] = {"Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"}; string Card::suits[] = {"hearts", "diamonds", "clubs", "spades"}; int Card::getFace(){return m_face;} void Card::setFace(int face){m_face = face;} int Card::getSuit(){return m_suit;} void Card::setSuit(int suit){m_suit = suit;} string Card::toString(string s_face, string s_suit) { string card = s_face + "\tof\t " + s_suit; return card; } #include &lt;iostream&gt; // cout #include &lt;algorithm&gt; // random_shuffle #include &lt;vector&gt; // vector #include &lt;ctime&gt; // time #include &lt;cstdlib&gt; #include "Card.h" using namespace std; class deckOfCards { private: vector&lt;Card&gt; deck; public: deckOfCards(); static int count; static int next; vector&lt;Card&gt;&amp; getDeck() {return deck;} void shuffle(vector&lt;Card&gt;&amp; deck); Card dealCard(); bool moreCards(); void pairs(vector&lt;Card&gt;&amp; fiveHand); void threeOfKind(vector&lt;Card&gt;&amp; fiveHand); void fourOfKind(vector&lt;Card&gt;&amp; fiveHand); void flush(vector&lt;Card&gt;&amp; fiveHand); void straight(vector&lt;Card&gt;&amp; fiveHand); bool sortFunc(vector&lt;Card&gt;&amp; fiveHand[i], vector&lt;Card&gt;&amp; fiveHand[j]); }; int deckOfCards::count = 0; int deckOfCards::next = 0; deckOfCards::deckOfCards() { const int FACES = 13; const int SUITS = 4; for (int suit = 0; suit &lt; SUITS; suit++) { for (int face = 0; face &lt; FACES; face++) { Card card = Card(face, suit); //card created and initialized deck.push_back(card); } } } void deckOfCards::shuffle(vector&lt;Card&gt;&amp; deck) { srand(static_cast&lt;unsigned int&gt;(time(0))); random_shuffle(deck.begin(), deck.end()); } Card deckOfCards::dealCard() { Card nextCard = deck[next]; next++; return nextCard; } bool deckOfCards::moreCards() { const int FIVE_CARD = 5; if (count &lt; FIVE_CARD) { count++; return true; } else return false; } void deckOfCards::pairs(vector&lt;Card&gt;&amp; fiveHand) { int pair = 0; const int MAX_CARDS = 5; for (int i = 0; i &lt; MAX_CARDS; i++) { int j = i+1; for (j; j &lt; MAX_CARDS; j++) { if (fiveHand[i].getFace() == fiveHand[j].getFace()) pair++; } } if (pair == 1) { cout &lt;&lt; "You have one pair!" &lt;&lt; endl; } else if (pair == 2) { cout &lt;&lt; "You have two pairs!" &lt;&lt; endl; } } void deckOfCards::threeOfKind(vector&lt;Card&gt;&amp; fiveHand) { if (((fiveHand[0].getFace() == fiveHand[1].getFace())&amp;&amp;(fiveHand[1].getFace() == fiveHand[2].getFace())) ||((fiveHand[1].getFace() == fiveHand[2].getFace())&amp;&amp;(fiveHand[2].getFace() == fiveHand[3].getFace())) ||((fiveHand[2].getFace() == fiveHand[3].getFace())&amp;&amp;(fiveHand[3].getFace() == fiveHand[4].getFace()))) { cout &lt;&lt; "Your hand contains three cards of the same faces!" &lt;&lt; endl; } } void deckOfCards::fourOfKind(vector&lt;Card&gt;&amp; fiveHand) { if (((fiveHand[0].getFace() == fiveHand[1].getFace()) &amp;&amp;(fiveHand[1].getFace()== fiveHand[2].getFace()) &amp;&amp;(fiveHand[2].getFace()== fiveHand[3].getFace())) ||((fiveHand[1].getFace() == fiveHand[2].getFace()) &amp;&amp;(fiveHand[2].getFace() == fiveHand[3].getFace()) &amp;&amp;(fiveHand[3].getFace() == fiveHand[4].getFace()))) { cout &lt;&lt; "Your hand contains four cards of the same faces!" &lt;&lt; endl; } } void deckOfCards::flush(vector&lt;Card&gt;&amp; fiveHand) { if ((fiveHand[0].getSuit() == fiveHand[1].getSuit()) &amp;&amp;(fiveHand[1].getSuit() == fiveHand[2].getSuit()) &amp;&amp;(fiveHand[2].getSuit() == fiveHand[3].getSuit()) &amp;&amp;(fiveHand[3].getSuit() == fiveHand[4].getSuit())) { cout &lt;&lt; "Congradulations!!! You have a flush!!!" &lt;&lt; endl; } } void deckOfCards::straight(vector&lt;Card&gt;&amp; fiveHand) { //sort cards based on faces sort(fiveHand.begin(), fiveHand.end(), sortFunc(vector&lt;Card&gt;&amp; fiveHand[i], vector&lt;Card&gt;&amp; fiveHand[j])); if ((fiveHand[1].getFace() == (fiveHand[0].getFace()+1)) &amp;&amp;(fiveHand[2].getFace() == (fiveHand[1].getFace()+1)) &amp;&amp;(fiveHand[3].getFace() == (fiveHand[2].getFace()+1)) &amp;&amp;(fiveHand[4].getFace() == (fiveHand[3].getFace()+1))) { cout &lt;&lt; "You're so lucky!!! You have a straight!" &lt;&lt; endl; } } bool deckOfCards::sortFunc(vector&lt;Card&gt;&amp; fiveHand[i], vector&lt;Card&gt;&amp; fiveHand[j]) { if (fiveHand[i].getFace() &lt; fiveHand[j].getFace()) return true; else return false; } #include "deckOfCards.h" using namespace std; int main(int argc, char** argv) { deckOfCards cardDeck; // create DeckOfCards object vector&lt;Card&gt; fiveHand; cardDeck.shuffle(cardDeck.getDeck()); // shuffle the cards in the deck cout &lt;&lt; "Your cards are: \n" &lt;&lt; endl; while (cardDeck.moreCards() == true) { Card currCard = cardDeck.dealCard(); fiveHand.push_back(currCard); string face = Card::faces[currCard.getFace()]; string suit = Card::suits[currCard.getSuit()]; string card = Card::toString(face,suit); cout &lt;&lt; card &lt;&lt; endl; } cout &lt;&lt; endl; cardDeck.pairs(fiveHand); cardDeck.threeOfKind(fiveHand); cardDeck.fourOfKind(fiveHand); cardDeck.flush(fiveHand); cardDeck.straight(fiveHand); return 0; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload