错误:请求“y”中的成员“x”,该成员属于非类类型“class**”
error: request for member ‘x’ in ‘y’, which is of non-class type ‘Class**’
我现在正试图创建一个"战争"游戏,我只是想设置甲板,但我收到了错误:
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中,参数卡的类型为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中,您调用提供Deck *
类型参数的函数makeDeck,因为cards[52]
是具有索引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
所以你所做的就是你所得到的。:)
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 对RValue对象调用的LValue ref限定成员函数
- Qt C++ 如何在 1 个命令中砍掉属于 QStringList 成员的 QString 字母
- 获取"rw_ssid"中成员"长度"的错误请求,该成员属于非类类型"char*"
- 基类私有成员属于派生类的哪个部分?
- 如何找出成员函数属于哪个类
- C++属于模板类的成员模板的显式专用化
- 请求 中的成员,属于非类类型
- 错误:请求 中的成员属于非类类型
- 请求在"伺服控制器"中请求成员"附加",该成员属于非类类型"int"
- 请求 [..] 中的成员 [..],该成员属于非类类型 [..]
- 错误:在“连接”中请求成员“板”,该“板”属于非类类型“游戏*”
- C++ - 映射 - 错误:请求"w"中的成员'first',该成员属于非类类型 'const int'
- 错误:请求“y”中的成员“x”,该成员属于非类类型“class**”
- 错误:请求"cur"中的成员"成员",该成员属于非类类型节点<int>* 二叉树
- 错误:请求成员 ..属于非类类型
- 请求“x”中的成员“insert”,该成员属于非类类型
- 请求“this”中的成员“mCar”,该成员属于非类类型“owner*const”
- C++:如何调用属于同一类的成员函数指针
- C++ 请求成员,属于非类类型(使用类模板时,无法在主中定义)