【问题标题】:error: request for member ‘x’ in ‘y’, which is of non-class type ‘Class**’错误:请求“y”中的成员“x”,它是非类类型“Class**”
【发布时间】: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
  • 它们很相似,但我做了这些更改以使我的错误更小......并且向量还没有被覆盖(从书中做一个作业,展望未来他们会让我把它改成一个矢量看起来像......)

标签: c++ class pointers


【解决方案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,表达式 &cards[52] 是它的地址,而函数 makeDeck 具有 @ 类型的参数987654325@

void makeDeck(Deck *cards[52])

//...

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

  Deck cards[52];

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

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

【讨论】:

  • 所以我修复了 showName();错误:[int q = cards[r]->faceNum;] 但不是“makeDeck(&cards[52]);”传递整个数组?
  • @Gamer_Xolin_:不!它正在将地址传递到数组的末尾。元素cards[52] 不存在,仅适用于开始/结束样式的迭代。 for( Deck *it = &amp;cards[0], *end = &amp;cards[52]; it &lt; end; ++it )
  • 如果你写&amp;cards,你会得到一个指向整个数组的指针,它的类型是指向数组的指针,这意味着你的函数需要是makeDeck( Deck (*cards)[52] )——括号很重要.您可以将其用作(*cards)[x].suit。这很难看,因此您应该只调用makeDeck(cards) 并将函数编写为makeDeck( Deck* cards ),然后使用cards[x].suit
  • @Gamer_Xolin 要传递整个数组,您必须编写 makeDeck(cards, 52);并且函数 makeDeck 必须声明为 makeDeck( Deck *, size_t n );
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多