简体   繁体   English

向量的内容不会打印

[英]contents of vector won't print

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). 'deck'是Card类的矢量(用于纸牌游戏)。 The error is coming from the first '<<' in the cout line. 错误来自cout行中的第一个“ <<”。 Visual Studio is saying "no operator "<<" matches these operands - operand types are: std::ostream < < Card". Visual Studio说“没有运算符“ <<”匹配这些操作数-操作数类型为:std :: ostream <<Card”。 I'm posting this as a new question because I have included <string> , <iostream> , and using namespace std; 我将其发布为一个新问题,因为我包含了<string><iostream>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. 据我所知,我的语法是正确的,但是我对C ++还是比较陌生,因此可能只是用户错误。

Thanks in advance! 提前致谢!

EDIT: here's the Card class header file: 编辑:这是Card类头文件:

#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: 这是Card .cpp文件:

#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);
        }

    }

Since you are relatively new to C++ , I think there is a misunderstanding in the comments 由于您是C ++的新手 ,所以我认为注释中存在误解

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 我在Card类中定义了ostream和iostream,AIhand向量使用了[...],但似乎没有什么不同

what others ask about is, whether you have defined a custom ostream operator << for the Card class. 其他人问的是,是否为Card类定义了自定义的ostream operator << What you answer is, that you have included the ostream and iostream header. 您的回答是,您已经包含了ostreamiostream标头。

Simple solution: try to print text instead of your Card class: 简单的解决方案:尝试打印文本而不是Card类:

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

More sophisticated solution: inform yourself how to overload the operator << for your card class. 更复杂的解决方案:告知您如何为卡类超载operator <<

See those related questions for more information: 有关更多信息,请参见那些相关问题:

Print function for class c++ C ++类的打印功能

How to properly overload the << operator for an ostream? 如何正确重载<<运算符以进行ostream?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM