C++传递结构体指针报错求助:扑克游戏代码指针问题
C++扑克游戏指针错误排查
我是C++初学者,确定问题出在对指针的理解上。现在写一款扑克游戏,card是包含suit和value的结构体,玩家手牌和公共牌都用这个结构体填充。尝试把数组引用传给函数,但数组指针的写法肯定有问题,简化后的代码和报错如下:
#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); }
报错信息
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.
问题分析与修复
你的核心问题是数组传递时的指针类型匹配错误,以及对数组访问方式的误解,一步步修正:
修正函数参数类型
你声明的hands是二维数组card[num_players][2],传递给函数时不需要用const card **这种复杂的指针,直接用数组引用更清晰(C++11及以上支持),能明确保留数组的大小信息。同理mid_cards是一维数组,参数不需要写成指针数组:void get_winner(const card (&hands)[num_players][2], const card (&mid_cards)[5])修正数组访问与输出逻辑
- 原代码中
mid_cards[i][0]完全错误,mid_cards是一维数组,每个元素是card结构体,不是指针数组,不需要解引用*。 - 直接输出
card结构体对象会报错,因为std::ostream默认不支持结构体输出,需要重载<<运算符。
- 原代码中
添加结构体输出重载
在card结构体定义后添加:std::ostream& operator<<(std::ostream& os, const card& c) { const char* suit_names[] = {"hearts", "diamonds", "spades", "clubs"}; const char* value_names[] = {"two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "jack", "queen", "king", "ace"}; os << value_names[c.v] << " of " << suit_names[c.s]; return os; }修正函数调用
参数改为数组引用后,直接传数组名即可,不需要取地址:get_winner(hands, mid_cards);
完整修复后的代码
#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; }; // 重载<<运算符用于输出card结构体 std::ostream& operator<<(std::ostream& os, const card& c) { const char* suit_names[] = {"hearts", "diamonds", "spades", "clubs"}; const char* value_names[] = {"two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "jack", "queen", "king", "ace"}; os << value_names[c.v] << " of " << suit_names[c.s]; return os; } static const int num_players = 4; // 使用数组引用作为参数 void get_winner(const card (&hands)[num_players][2], const card (&mid_cards)[5]) { std::cout << "Middle cards:\n"; for (int i = 0; i < 5; i++) { std::cout << "Middle card " << i + 1 << ": " << mid_cards[i] << "\n"; } std::cout << "\nPlayers' hands:\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); }
内容的提问来源于stack exchange,提问作者kjdfsjdsa kafsjna
相关产品推荐
相关产品推荐

