简体   繁体   English

无法使用 get 函数返回对象数组

[英]Can't return an array of object with a get function

I'm programming a simple blackjack game for a university exam.我正在为大学考试编写一个简单的二十一点游戏。 The game has no graphic, it print everybody on the console.游戏没有图形,它会在控制台上打印每个人。 The project has three classes: Deck, Card, Special Card.该项目分为三类:Deck、Card、Special Card。 I want to show you only the Deck class because it's there my problem.我只想向您展示 Deck 课程,因为这正是我的问题所在。 In Deck class there is a paramater deck which is an array of Cards (array of object) "Card * deck[52]" it's the definition.在 Deck 类中,有一个参数卡片组,它是一组卡片(对象数组)“卡片 * 卡片组 [52]”,它是定义。 In the function buildDeck() this array of Card is implemented (every location of the array is filled with different Cards. Now I want to have a function that return this deck so I can use it in the main. I post my code of Deck.h and Deck.cpp在函数 buildDeck() 中实现了这个 Card 数组(数组的每个位置都填充了不同的 Cards。现在我想要一个返回这个卡片组的函数,以便我可以在 main 中使用它。我发布了我的 Deck 代码.h 和 Deck.cpp

DECK.H
#ifndef DECK_H
#define DECK_H
#include "Card.h"

class Deck
{
    public:
        Deck();
        void buildDeck();
        void mixDeck();
        Card * getDeck();

    private:
        const char * H = "hearts";
        const char * D = "diamonds";
        const char * C = "clubs";
        const char * S = "spades";
        Card * deck[52];
};

#endif // DECK_H

And here it is the .cpp这是 .cpp

#include <iostream>
#include "Deck.h"
#include "Card.h"
#include "Special_Card.h"
using namespace std;

Deck::Deck(){}

void Deck::buildDeck(){
    //here we got a series of for statements which create the deck 
    //and assign every location of the array to a different card. I 
    //won't post this part to keep the code simple
}

Card * Deck::getDeck(){
    return deck;  //HERE I GOT THE ERROR
}

When I try to build the application at the line 57 (the line where I put the comment "//HERE I GOT THE ERROR") I got this error:当我尝试在第 57 行(我在其中添加注释“//HERE I GOT THE ERROR”的那一行)构建应用程序时,我收到此错误:

error: cannot convert 'Card**' to 'Card*' in return

Have you got any idea on how to fix this?你知道如何解决这个问题吗?

you declared deck as an array of pointers to class Card while you want to return a pointer to class card so you can either return one element from yhe array deck:您将deck 声明为指向类 Card 的指针数组,而您想返回一个指向类 card 的指针,以便您可以从 yhe 数组deck 中返回一个元素:

Card * Deck::getDeck(){
           return deck[0];  // or any other element
 }

or if you want to return the whole array of pointers you can declare the function as:或者如果你想返回整个指针数组,你可以将函数声明为:

Card** Deck::getDeck(){
    return deck;  //
}

Just to answer your question: if you declare member variable deck as Card * deck[52] , then this denotes an array of 52 pointers to card objects, whereas a signature Card* getDeck() indicates a single pointer to a card object, not a pointer to an array of pointers.只是为了回答您的问题:如果您将成员变量deck声明为Card * deck[52] ,那么这表示一个包含 52 个指向卡片对象的指针的数组,而签名Card* getDeck()表示指向卡片对象的单个指针,而不是一个指向指针数组的指针。

Hence, to get rid of the error, you would have to write因此,要摆脱错误,您必须编写

Card ** getDeck();

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

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