gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 02:13:08 26 4
gpt4 key购买 nike

我正在为大学考试编写一个简单的二十一点游戏。游戏没有图形,它在控制台上打印每个人。该项目分为三个类:Deck、Card、Special Card。我只想向您展示 Deck 类,因为这是我的问题。在 Deck 类中有一个参数甲板,它是一个卡片数组(对象数组)“Card * deck[52]”,这是定义。在函数 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

这是.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
}

当我尝试在第 57 行(我放置注释“//HERE I GOT THE ERROR”的那一行)构建应用程序时,我得到了这个错误:

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

您知道如何解决这个问题吗?

最佳答案

您将 deck 声明为指向 Card 类的指针数组,同时您想要返回指向 card 类的指针,以便您可以从数组 deck 返回一个元素:

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

或者如果您想返回整个指针数组,您可以将函数声明为:

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

关于c++ - 无法使用 get 函数返回对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41794314/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com