我是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.
发布于 2022-08-13 22:17:27
在函数的参数中,您使用了错误的语法来接受指针数组。而且,您为数组声明了错误的元素类型(您声明了card*
指针,但是数组实际上持有card
对象)。而且,当mid_cards
中的实际数组是一维数组时,您的函数将main()
参数视为指向二维数组的指针。
此外,您没有为您的operator<<
结构定义一个card
,但是您正在尝试将card
值打印到cout
。
试一试:
#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);
}
或者,通过引用而不是指针传递数组,例如:
#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);
}
https://stackoverflow.com/questions/73347714
复制相似问题