2011-06-13 98 views
3

因此,在完成一本介绍性书籍后,我正在试着用一些C++语言,而且我陷入了困境。我制作了一个对象向量,每个对象都有一个SFML圆对象作为成员,并且我希望main()去绘制这些圆。该矢量称为theBoard,但是当我试图访问它,我得到以下错误信息:成员未在范围内声明?

error: request for member 'theBoard' in 'GameBoard', which is of non-class type 'Board*' 
error: 'theBoard' was not declared in this scope 

我是新来这个(就从这两年的Python),所以我相信我做了某处出现错误。下面是主板创造的相关代码:)

class Board 
{ 
public: 
    //These are the member functions. 
    Board(); 
    ~Board(); 
    vector<Space*> CreateBoard(); 
    //This will be the game board. 
    vector<Space*> theBoard; 
    //These clusters represent the waiting areas for pieces not yet in the game. 
    vector<Space*> Cluster1; 
    vector<Space*> Cluster2; 
    vector<Space*> Cluster3; 
private: 
    //These integers represent the number of spaces on each row, starting at the top  (which is row [0]) 
    vector<int> RowNums; 
}; 

Board::Board() 
{ 
    //Fill in RowNums with the right values. 
    RowNums.push_back(1); 
    RowNums.push_back(17); 
    RowNums.push_back(2); 
    RowNums.push_back(17); 
    RowNums.push_back(1); 
    RowNums.push_back(1); 
    RowNums.push_back(5); 
    RowNums.push_back(2); 
    RowNums.push_back(7); 
    RowNums.push_back(2); 
    RowNums.push_back(11); 
    RowNums.push_back(3); 
    RowNums.push_back(17); 
    RowNums.push_back(4); 
    RowNums.push_back(17); 
    //Then, create the board. 
    theBoard = CreateBoard(); 
} 

CreateBoard(是返回指针空间物体的向量一个很长很长的功能。我怀疑这里存在问题,因为当我尝试访问main()中的Space对象的圈子成员时,我收到唯一的错误消息。在我看来,好像我已在相关范围内宣布theBoard,即作为理事会类别的数据成员。

我的main()函数,在情况下,它是很重要的:

int main() 
{ 
    //This sets up the display window. 
    sf::RenderWindow App(sf::VideoMode(1200, 900, 32), "Malefiz"); 

    //This creates the board on the heap, and a pointer to it. 
    Board* GameBoard = new Board(); 

    cout << "Board made."; 

    //This is the game loop. 
    while(App.IsOpened()) 
    { 
     //This is used to poll events. 
     sf::Event Event; 
     while(App.GetEvent(Event)) 
     { 
      //This closes the window. 
      if(Event.Type == sf::Event::Closed) 
      { 
       App.Close(); 
      } 
     } 

     //This gets the time since the last frame. 
     //float ElapsedTime = App.GetFrameTime(); 

     //This fills the window with black. 
     App.Clear(sf::Color(200, 200, 125)); 
     //This draws the places into the window. 
     for(int i = 0; i < GameBoard.theBoard.size(); ++i) 
     { 
      App.Draw(GameBoard.*theBoard[i].m_Circle); 
     } 
     //This displays the window. 
     App.Display(); 
    } 

    return EXIT_SUCCESS; 
} 

回答

3

的错误,如果你仔细看这是很明确的。那就是:

Board *p = ... 
p.theBoard;  // Error, should be p->theBoard, as p is a pointer 

还要注意的是GameBoard.*theBoard[i].m_Circle可能不是你想要的,你可能要像GameBoard->theBoard[i]->m_Circle(因为有遗漏重要的位,我猜测)。

6

在你main()功能,GameBoardBoard *,而不是一个Board。因此,要访问成员,您需要使用->而不是.。例如: -

GameBoard->theBoard.size() 

[有些人(我也是其中之一),喜欢的领先pptr前缀来命名他们的指针变量,才能使这种刺激的明确清晰。]

+1

哦,可爱的系统匈牙利符号......(我讨厌它!):)我明白'p'的前缀,但从那里请:不。 – 2011-06-13 18:17:01

+0

@David:是的,这是我的匈牙利用法的限制。我认为'p'很重要,因为指针对所需的基本语法有如此巨大的影响。我同意所有'lpsz'微软的东西都是疯了! – 2011-06-13 19:21:15

3

GameBoard是一个指针,所以语法应该是这样的:

for(int i = 0; i < GameBoard->theBoard.size(); ++i) 
{ 
     App.Draw((GameBoard->theBoard[i])->m_Circle); 
} 

由于theBoard元件也是指针,所以访问m_Circle当我用指针表示法。

error: request for member 'theBoard' in 'GameBoard', which is of non-class type 'Board*' 
error: 'theBoard' was not declared in this scope 

第一行是告诉你,你有一个指向Board对象,您试图直接访问的成员:

4

GameBoard是指向Board对象的指针,因此您需要使用“ - >”运算符而不是“。”。运算符来访问其任何成员变量或方法。