简体   繁体   English

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

[英]Errors when passing pointer to structs c++

I am a beginner in C++, so I'm sure the error is from my misunderstanding of pointers.我是 C++ 的初学者,所以我确定错误是由于我对指针的误解。

I was making a Poker game, where card is a struct with the suit and the value , and each player's hand is filled with cards, and the middle is filled with cards.我正在制作一个扑克游戏,其中card是一个带有suitvalue的结构,每个玩家的手上都装满了纸牌,中间装满了纸牌。 I passed references of the arrays to a function by reference, but I think something is wrong with how I set up the pointers to the arrays.我通过引用将 arrays 的引用传递给了 function,但我认为我设置指向 arrays 的指针的方式有问题。

Here is a simplified version of my code:这是我的代码的简化版本:

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

Here are the error messages:以下是错误消息:

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.

In your function's parameters, you are using the wrong syntax to accept the arrays by pointer.在函数的参数中,您使用错误的语法通过指针接受 arrays。 And, you are declaring the wrong element type for the arrays (you claim card* pointers, but the arrays actual hold card objects instead).而且,您为 arrays 声明了错误的元素类型(您声明了card*指针,但 arrays 实际持有card对象)。 And, your function is treating the mid_cards parameter like it is a pointer to a 2-dimentional array when the actual array in main() is 1-dimensional instead.而且,当main()中的实际数组是一维时,您的 function 将mid_cards参数视为指向二维数组的指针。

Also, you do not have an operator<< defined for your card struct, but you are trying to print card values to cout .此外,您没有为您的card结构定义operator<< ,但您正在尝试将card值打印到cout

Try this instead:试试这个:

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

Online Demo在线演示

Alternatively, pass the arrays by reference instead of pointer, eg:或者,通过引用而不是指针传递 arrays,例如:

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

Online Demo在线演示

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

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