Urgent help on solitaire game project

I TRY TO PRINT OUT THE TOP CARD OF THE DECK BUT IT SAYING NO OPERATOR MATCHING << ERROR EVENTHOUGH I HAVE INCLUDE EVERYTHING I WOULD HAVE (IN MY BELIEVE). PLEASE HELP
#pragma once
#include <iostream>
#include <string>

using namespace std;
using std::string;
enum SUIT{ Heart, Club, Spade, Diamond };
enum VALUE {Ace = 1, Jack = 11, Queen = 12, King = 13};
class Card
{

public:
Card();
Card(int,int);
~Card();
void printCard();
int cardValue(string);
private:
SUIT suit;
VALUE value;
string Outputcharacter;
-------------
#include "stdafx.h"
#include "Card.h"

Card::Card()
{

}
Card::Card(int s,int v)
{
suit = (SUIT)s;
value = (VALUE)v;
}


Card::~Card()
{
}
---------------
#pragma once
#include <iostream>
#include <string>
#include<ctime>
#include "Card.h"
#include <vector>
using namespace std;
using std::string;
class Deck
{
public:
Deck();
~Deck();
void fillWholeDeck();
void shufflecard();
Card gettopcard();
private:

vector <Card> mDeck;
};

-----------------------
clude "stdafx.h"
#include "Deck.h"
#include <iostream>
#include <random>
#include <algorithm>
#include <string>


using namespace std;
using std::cout;

Deck::Deck()
{
}


Deck::~Deck()
{
}


void Deck::fillWholeDeck()
{
for (int s = 0; s < 4; s++){
for (int v = 1; v < 14; v++)
{
Card myCard(s, v);
mDeck.push_back(myCard);

}
}
/*for (vector<Card>::const_iterator i = mDeck.begin(); i != mDeck.end(); ++i)
std::cout << *i << ' ';*/
}
void Deck::shufflecard()
{
random_shuffle(mDeck.begin(), mDeck.end());
/*for (int i = 0; i < 52; i++)
cout << mDeck[i] << ;*/ ERROR NO OPERATOR << MATCH IN THIS LINE
}
//get the top card
Card Deck::gettopcard()
{
return mDeck[1];
}
-------------
#include "stdafx.h"
#include <iostream>
#include <string>
#include <ctime>
#include "cardholder.h"
#include "rule.h"
#include "Card.h"
#include "Deck.h"

using namespace std;
using std::cout;
using std::cin;
using std::string;

int _tmain(int argc, _TCHAR* argv[])
{
Deck a;
a.fillWholeDeck();
a.shufflecard();
cout << a.gettopcard() << endl;

return 0;
}
I TRY TO PRINT OUT THE TOP CARD OF THE DECK BUT IT SAYING NO OPERATOR MATCHING << ERROR EVENTHOUGH I HAVE INCLUDE EVERYTHING I WOULD HAVE (IN MY BELIEVE). PLEASE HELP
cout << a.gettopcard() << endl;

a.gettopcard() returns a Card object. Your Card class does not overload the << operator.
C++ does not know how to output a Card object unless you overload the << operator.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Last edited on
Thank you!!!
i put this in my Deck object

1
2
3
4
5
ostream& operator<< (std::ostream& stream, const Deck& Mydeck)
{
    stream << Mydeck.mDeck;
	return stream;
}

it still saying NO OPERATOR MATCHING << ERROR right at the stream <<
How do i fix this.. Thanks you
gettopcard() returns a Card object for which the insertion operator << has not been overloaded. You need to do this, one approach is with a friend function, something
on the lines of:
1
2
3
4
5
ostream& operator << (ostream& os, const Card& c)
{ 
	os << "Suit: " << c.suit << "Value: " << c.value << '\n';
	return os;
}


Further note data members suit and value are type enum SUIT and VALUE respectively. So when you print c.suit and say the SUIT was Club it would print 1 and so on.
If you wanted to print it out in words then you'd additionally need to have a switch case to print text from the enum:
http://stackoverflow.com/questions/3168306/print-text-instead-of-value-from-c-enum
Thank you
but it still saying NO OPERATOR MATCHING << ERROR right at the stream <<
How do i fix this..
1
2
3
4
5
ostream& operator<< (std::ostream& stream, const Deck& Mydeck)
{
    stream << Mydeck.mDeck;
	return stream;
}

I am also try your code but same problem. i put the code in my Deck.cpp or should i do it in Card.cpp
i put this in my Deck object

Two problems.
1) Your snippet is for outputting a Deck object. gettopcard() returns a Card object. Your snippet is not going to work for a Card object.

2) Line 3: Mydeck.mDeck is a vector<Card>. Again, C++ does not know how to print out a vector. You have to provide logic to iterate through the vector and print the members.

how do i fix this problem...sorry i have less experience with c++..that s why i need a lot of help
Sorry, but I had to put this in a single file to work on it. I have also inlined shorter functions.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "stdafx.h"           // Comment out if not Visual Studio
#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include <algorithm>

using namespace std;

string SUIT[] = { "Heart", "Club", "Spade", "Diamond" };        // <===== CHANGED TO strings 
string VALUE[] = { "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };


//=====================================================================


class Card
{
public:
   Card() {}
   Card( int s, int v ) { suit = s; value = v; }                // <===== Changed to int
   ~Card() {}
   void printCard();                                            // <===== WARNING: Not yet set
   int cardValue(string);                                       // <===== WARNING: Not yet set
   friend ostream &operator<< ( ostream &os, const Card &c );   // <===== Needed to access private members suit and value
private:
   int suit;                                                    // <===== Changed to int
   int value;
   string Outputcharacter;
};


ostream &operator<< ( ostream &os, const Card &c )              // <===== THIS IS WHAT gunnerfunner AND AbstractionAnon ARE REPEATEDLY TELLING YOU
{                                                               //        OPERATOR << NEEDS OVERLOADING FOR A Card, NOT A Deck *****     
   os << "Suit: " << SUIT[c.suit] << "   Value: " << VALUE[c.value] << '\n';
   return os;           
}                                                                


//=====================================================================


class Deck
{
public:
   Deck() {}
   ~Deck() {}
   void fillWholeDeck();
   void shufflecard() { random_shuffle( mDeck.begin(), mDeck.end() ); }
   Card gettopcard() { return mDeck[0]; }              // <===== NOTE THAT THIS IS RETURNING A ***** Card *****
private:
   vector <Card> mDeck;
};


void Deck::fillWholeDeck()
{
   for (int s = 0; s < 4; s++)                         // CHECK LOOP LIMIT
   {
      for (int v = 0; v < 13; v++)                     // DITTO
      {
         Card myCard( s, v );
         mDeck.push_back( myCard );
      }
   }
}


//=====================================================================


int main()
{
   Deck a;
   a.fillWholeDeck();

   srand( time(0) );
   a.shufflecard();

   cout << a.gettopcard() << endl;
}
Last edited on
What a majestic solution! OP: you are one lucky chappie
Topic archived. No new replies allowed.