gpt4 book ai didi

c++ - 错误 : request for member ‘x’ in ‘y’ , 是非类类型 ‘Class**’

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:19:19 25 4
gpt4 key购买 nike

我正试图创建一个“ war ”游戏,现在我只是想设置套牌,但我遇到了错误:

Running /home/ubuntu/workspace/Testing__.cc
/home/ubuntu/workspace/Testing__.cc: In function ‘void showName(Deck**)’:
/home/ubuntu/workspace/Testing__.cc:72:19: error: request for member ‘faceNum’ in ‘cards’, which is of non-class type ‘Deck**’
int q = cards.faceNum;
^
/home/ubuntu/workspace/Testing__.cc:73:22: error: request for member ‘suit’ in ‘cards’, which is of non-class type ‘Deck**’
string s = cards.suit;
^
/home/ubuntu/workspace/Testing__.cc: In function ‘int main()’:
/home/ubuntu/workspace/Testing__.cc:94:22: error: cannot convert ‘Deck*’ to ‘Deck**’ for argument ‘1’ to ‘void makeDeck(Deck**)’
makeDeck(&cards[52]);
^
/home/ubuntu/workspace/Testing__.cc:102:27: error: cannot convert ‘Deck*’ to ‘Deck**’ for argument ‘1’ to ‘void showName(Deck**)’
showName(&cards[(r-1)]);

这是我到目前为止的代码:

#include <iostream>
#include <cstdlib> //system("PAUSE");
#include <string>
#include <stdio.h> //NULL
#include <stdlib.h> // srand, rand
#include <ctime> // time
using namespace std;

class Deck
{
public:
int faceNum;
string suit;

friend ostream& operator<<(ostream& os, const Deck& cards);
};

ostream& operator<<(ostream& os, const Deck& cards)
{
os << cards.faceNum << " of " << cards.suit;
return os;
}


void makeDeck(Deck *cards[52])
{
//creates the deck, Ace -> King (1-13) of each suit
int n=1;
for (int x=0; x<13; x++)
{
cards[x]->faceNum = n;
cards[x]->suit = "Spades";
n++;
}
n=1;
for (int x=13; x<26; x++)
{
cards[x]->faceNum = n;
cards[x]->suit = "Clubs";
n++;
}
n=1;
for (int x=26; x<39; x++)
{
cards[x]->faceNum = n;
cards[x]->suit = "Diamonds";
n++;
}
n=1;
for (int x=39; x<52; x++)
{
cards[x]->faceNum = n;
cards[x]->suit = "Hearts";
n++;
}

}

/*
void selectCard()
{
//each player will use this
}
*/

void showName(Deck *cards[52]) //displays name based on card.faceNum
{
for(int c=0;c<52;c++)
{
int q = cards.faceNum; ///error here
string s = cards.suit; ///error here
if(q == 1)
cout << "Ace of " << s;
else if(q >= 2 && q <= 10)
cout << q << " of " << s;
else if(q == 11)
cout << "Jack of " << s;
else if(q == 12)
cout << "Queen of " << s;
else if(q == 13)
cout << "King of " << s;
}
}

int main()
{
int r;
int n=1;

Deck cards[52];

makeDeck(&cards[52]); ///error here

//Show me "this" card.....
do
{
cout << "(99 will exit)\nShow card (1-52): ";
cin >> r;
//cout << cards[(r-1)] << endl;
showName(&cards[(r-1)]); ///error here

}while (r != 99);


//welcome!!
//srand((unsigned)time(NULL)); //rand cast based on time



//r = rand() % dtype + 1;

//create deck

//use selectCard() to randomly pull from pile
}

(使用 Cloud9 (c9.io))现在还早,所以整个程序还不完整,但我应该可以选择一张卡片 (1-52) 并显示它:

4 of Clubs

我从谷歌搜索中发现(到目前为止)我的指针有问题……我想……我把这一切都放在了 main() 函数中并且它起作用了,我把它分成了单独的函数、较小的代码编辑和出现的错误。

我正在努力思考指针,因此非常感谢任何帮助/建议。我确定这是一个简单的 3 秒答案,但我真的很难过....

谢谢大家!

最佳答案

错误信息很清楚。例如,在函数 showName 中,参数 cards 的类型为 Deck **,这是一个指向指针的指针。因此,您不能使用成员访问运算符。

void showName(Deck *cards[52]) //displays name based on card.faceNum
{
for(int c=0;c<52;c++)
{
int q = cards.faceNum; ///error here
string s = cards.suit; ///error here

在函数 main 中调用函数 makeDeck 提供 Deck * 类型的参数,因为 cards[52] 是 Deck 类型数组的一个元素,索引为 52,表达式为 &cards [52] 是它的地址,而函数 makeDeck 的参数类型为 Deck **

void makeDeck(Deck *cards[52])

//...

int main()
{
int r;
int n=1;

Deck cards[52];

makeDeck(&cards[52]); ///error here

所以你所做的就是你得到的。:)

关于c++ - 错误 : request for member ‘x’ in ‘y’ , 是非类类型 ‘Class**’,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28863935/

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