首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将指针传递给结构c++时出错

将指针传递给结构c++时出错
EN

Stack Overflow用户
提问于 2022-08-13 21:21:44
回答 1查看 65关注 0票数 0

我是C++的初学者,所以我确信错误是因为我对指针的误解。

我在做扑克游戏,card是一个suitvalue的结构,每个玩家的手都装满了卡片,中间充满了卡片。我通过引用将数组的引用传递给函数,但我认为设置指向数组的指针的方式有问题。

下面是我的代码的简化版本:

代码语言:javascript
运行
复制
#include <iostream>
enum suit {hearts, diamonds, spades, clubs};
enum value {two, three, four, five, six, seven, eight, nine, ten, jack, queen, king, ace};

struct card 
{
    value v;
    suit s;
};
static const int num_players = 4;

void get_winner(const card ** hands[num_players][2], const card * mid_cards[5])
{
    for (int i = 0; i < 5; i++) {
        std::cout << "Middle card " << i + 1 << ": " << * mid_cards[i][0] << ", " << * mid_cards[i][1] << "\n";
    }

    for (int i = 0; i < num_players; i++) {
        std::cout << "Player " << i + 1 << "hand: " << * hands[i][0] << ", " << * hands[i][1] << "\n";
    }
}

int main()
{
    card hands[num_players][2];
    card mid_cards[5];
    // fill hands and mid_cards with card structs
    hands[0][0] = card {jack, hearts};
    hands[0][1] = card {nine, spades};
    hands[1][0] = card {ace, clubs};
    hands[1][1] = card {ace, hearts};
    hands[2][0] = card {three, diamonds};
    hands[2][1] = card {seven, spades};
    hands[3][0] = card {eight, diamonds};
    hands[3][1] = card {nine, clubs};
    mid_cards[0] = card {five, clubs};
    mid_cards[1] = card {king, hearts};
    mid_cards[2] = card {queen, spades};
    mid_cards[3] = card {two, clubs};
    mid_cards[4] = card {ace, hearts};
    get_winner(&hands, &mid_cards);
}

以下是错误消息:

代码语言:javascript
运行
复制
main.cpp:17:51: error: indirection requires pointer operand
      ('const card' invalid)
  ...<< "Middle card " << i + 1 << ": " << * mid_cards[i][0] << ", " << * mid...
                                           ^ ~~~~~~~~~~~~~~~
main.cpp:17:80: error: indirection requires pointer operand
      ('const card' invalid)
  ...i + 1 << ": " << * mid_cards[i][0] << ", " << * mid_cards[i][1] << "\n";
                                                   ^ ~~~~~~~~~~~~~~~
main.cpp:42:2: error: no matching function for call to 'get_winner'
        get_winner(&hands, &mid_cards);
        ^~~~~~~~~~
main.cpp:14:6: note: candidate function not viable: no known conversion from
      'card (*)[4][2]' to 'const card **(*)[2]' for 1st argument
void get_winner(const card ** hands[num_players][2], const card * mid_cards[5])
     ^
3 errors generated.
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-08-13 22:17:27

在函数的参数中,您使用了错误的语法来接受指针数组。而且,您为数组声明了错误的元素类型(您声明了card*指针,但是数组实际上持有card对象)。而且,当mid_cards中的实际数组是一维数组时,您的函数将main()参数视为指向二维数组的指针。

此外,您没有为您的operator<<结构定义一个card,但是您正在尝试将card值打印到cout

试一试:

代码语言:javascript
运行
复制
#include <iostream>

enum suit {hearts, diamonds, spades, clubs};
enum value {two, three, four, five, six, seven, eight, nine, ten, jack, queen, king, ace};

struct card 
{
    value v;
    suit s;
};
static const int num_players = 4;

static const char* suits[] = {"Hearts", "Diamonds", "Spades", "Clubs"};
static const char* values[] = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"};

std::ostream& operator<<(std::ostream &out, const card &c)
{
    out << values[c.v] << " of " << suits[c.s];
    return out;
}

void get_winner(const card (*hands)[num_players][2], const card (*mid_cards)[5])
{
    for (int i = 0; i < 5; i++) {
        std::cout << "Middle card " << i + 1 << ": " << (*mid_cards)[i] << "\n";
    }

    for (int i = 0; i < num_players; i++) {
        std::cout << "Player " << i + 1 << "hand: " << (*hands)[i][0] << ", " << (*hands)[i][1] << "\n";
    }
}

int main()
{
    card hands[num_players][2];
    card mid_cards[5];
    // fill hands and mid_cards with card structs
    hands[0][0] = card {jack, hearts};
    hands[0][1] = card {nine, spades};
    hands[1][0] = card {ace, clubs};
    hands[1][1] = card {ace, hearts};
    hands[2][0] = card {three, diamonds};
    hands[2][1] = card {seven, spades};
    hands[3][0] = card {eight, diamonds};
    hands[3][1] = card {nine, clubs};
    mid_cards[0] = card {five, clubs};
    mid_cards[1] = card {king, hearts};
    mid_cards[2] = card {queen, spades};
    mid_cards[3] = card {two, clubs};
    mid_cards[4] = card {ace, hearts};
    get_winner(&hands, &mid_cards);
}

在线演示

或者,通过引用而不是指针传递数组,例如:

代码语言:javascript
运行
复制
#include <iostream>

enum suit {hearts, diamonds, spades, clubs};
enum value {two, three, four, five, six, seven, eight, nine, ten, jack, queen, king, ace};

struct card 
{
    value v;
    suit s;
};
static const int num_players = 4;

static const char* suits[] = {"Hearts", "Diamonds", "Spades", "Clubs"};
static const char* values[] = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"};

std::ostream& operator<<(std::ostream &out, const card &c)
{
    out << values[c.v] << " of " << suits[c.s];
    return out;
}

void get_winner(const card (&hands)[num_players][2], const card (&mid_cards)[5])
{
    for (int i = 0; i < 5; i++) {
        std::cout << "Middle card " << i + 1 << ": " << mid_cards[i] << "\n";
    }

    for (int i = 0; i < num_players; i++) {
        std::cout << "Player " << i + 1 << "hand: " << hands[i][0] << ", " << hands[i][1] << "\n";
    }
}

int main()
{
    card hands[num_players][2];
    card mid_cards[5];
    // fill hands and mid_cards with card structs
    hands[0][0] = card {jack, hearts};
    hands[0][1] = card {nine, spades};
    hands[1][0] = card {ace, clubs};
    hands[1][1] = card {ace, hearts};
    hands[2][0] = card {three, diamonds};
    hands[2][1] = card {seven, spades};
    hands[3][0] = card {eight, diamonds};
    hands[3][1] = card {nine, clubs};
    mid_cards[0] = card {five, clubs};
    mid_cards[1] = card {king, hearts};
    mid_cards[2] = card {queen, spades};
    mid_cards[3] = card {two, clubs};
    mid_cards[4] = card {ace, hearts};
    get_winner(hands, mid_cards);
}

在线演示

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73347714

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档