0

So I'm trying to just print the contents of a simple vector, but I'm getting a weird error. Here is the code:

srand(time(NULL));

for (int i = 0; i < 7; i++){
    AIhand[i] = deck[rand() % deck.size()];
    cout << AIhand[i] << endl;
}

'deck' is a vector of a Card class (it's for a card game). The error is coming from the first '<<' in the cout line. Visual Studio is saying "no operator "<<" matches these operands - operand types are: std::ostream < < Card". I'm posting this as a new question because I have included <string>, <iostream>, and using namespace std;, and those are the usual solutions to people's problems of not being able to print a vector.

As far as I can tell my syntax is right, but I'm relatively new to C++, so it might just be user error.

Thanks in advance!

EDIT: here's the Card class header file:

#ifndef CARD_H_
#define CARD_H_

#include <string>
#include <iostream>
#include <ostream>
#include <vector>
using namespace std;

class Card {
public:
    Card(string newSuit, int newValue);
    string showCard();

private:
    int cardValue;
    string cardSuit;
};


#endif CARD_H_

here's the Card .cpp file:

#include "Card.h"
#include <sstream>
#include <iostream>
#include <ostream>

Card::Card(string newSuit, int newValue) {

    cardValue = newValue;

    cardSuit = newSuit;

}

string Card::showCard(){

    stringstream card;

    card << cardValue << " of " << cardSuit << endl;

    return card.str();
}

this is the deck

vector<Card> deck;

    for (int i = 0; i < 56; i++){
        for (int j = 0; j < 14; j++) {
            Card cuccos("Cuccos", j);
            deck.push_back(cuccos);
        }
        for (int j = 0; j < 14; j++){
            Card loftwings("Loftwings", j);
            deck.push_back(loftwings);
        }
        for (int j = 0; j < 14; j++){
            Card bullbos("Bullbos", j);
            deck.push_back(bullbos);
        }
        for (int j = 0; j < 14; j++){
            Card skulltulas("Skulltulas", j);
            deck.push_back(skulltulas);
        }

    }
marina
  • 3
  • 2
  • 1
    Possible duplicate of [Print function for class c++](https://stackoverflow.com/questions/23239646/print-function-for-class-c) – Ken Y-N Sep 15 '17 at 05:44
  • 1
    did you define the **<< operator** for the class **AIhand**?? – ΦXocę 웃 Пepeúpa ツ Sep 15 '17 at 05:45
  • @ΦXocę웃Пepeúpaツ I have ostream and iostream defined in the Card class that the AIhand vector uses – marina Sep 15 '17 at 05:50
  • 1
    According to my understanding, deck is a vector of card class, so have you defined `<< operator` for card class? – Shubham Agrawal Sep 15 '17 at 06:02
  • @ShubhamAgrawal I have, but it doesn't seem to have made a difference – marina Sep 15 '17 at 06:06
  • 3
    then plz share the whole code of operator overloading and class – Shubham Agrawal Sep 15 '17 at 06:09
  • @marina If your question is answered, consider accepting the answer (See [How does accepting an answer work?](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)). Otherwise you should probably improve your question to clarify your current implementation of the output operator. – grek40 Sep 15 '17 at 12:19

1 Answers1

1

Since you are relatively new to C++, I think there is a misunderstanding in the comments

I have ostream and iostream defined in the Card class that the AIhand vector uses [...] but it doesn't seem to have made a difference

what others ask about is, whether you have defined a custom ostream operator << for the Card class. What you answer is, that you have included the ostream and iostream header.

Simple solution: try to print text instead of your Card class:

cout << AIhand[i].showCard() << endl;

More sophisticated solution: inform yourself how to overload the operator << for your card class.

See those related questions for more information:

Print function for class c++

How to properly overload the << operator for an ostream?

grek40
  • 13,113
  • 1
  • 24
  • 50