错误:请求“y”中的成员“x”,该成员属于非类类型“class**”

error: request for member ‘x’ in ‘y’, which is of non-class type ‘Class**’

本文关键字:成员 属于 类型 class 请求 错误      更新时间:2023-10-16

我现在正试图创建一个"战争"游戏,我只是想设置甲板,但我收到了错误:

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

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