【发布时间】:2015-03-04 20:10:37
【问题描述】:
我正在尝试创建一个“战争”游戏,现在我只是在尝试设置套牌,但我遇到了错误:
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 秒答案,但我真的很难过....
谢谢大家!
【问题讨论】:
-
您的函数
makeDeck表明您知道访问声明为Deck *cards[52]的cards数组的正确语法。为什么会突然切换到showName@中的一些完全不同且无意义的语法??? -
您应该消除传递数组的复杂性并使用
std::vector。 -
它们很相似,但我做了这些更改以使我的错误更小......并且向量还没有被覆盖(从书中做一个作业,展望未来他们会让我把它改成一个矢量看起来像......)