我试图创建一个“战争”的游戏,现在我只是试图让甲板设置,并且我得到错误:
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秒答案,但我真的很难过......
全部谢谢!
答案 0 :(得分:1)
错误消息足够清晰。例如,在函数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中,调用函数makeDeck提供类型为Deck *
的参数,因为cards[52]
是Deck类型数组的元素,索引为52,表达式&amp; cards [52]是其地址而函数makeDeck的参数类型为Deck **
void makeDeck(Deck *cards[52])
//...
int main()
{
int r;
int n=1;
Deck cards[52];
makeDeck(&cards[52]); ///error here
所以你所做的就是你得到的。:)