Reputation: 3
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);
}
}
Upvotes: 0
Views: 159
Reputation: 13458
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:
How to properly overload the << operator for an ostream?
Upvotes: 1