2011-06-13 73 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對象的指針,因此您需要使用「 - >」運算符而不是「。」。運算符來訪問其任何成員變量或方法。