【问题标题】:Member not declared in scope?成员未在范围内声明?
【发布时间】:2011-09-14 03:04:25
【问题描述】:

所以我在读完一本入门书后尝试了一些 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() 是一个非常非常长的函数,它返回指向 Space 对象的指针向量。我怀疑这里有问题,因为当我尝试访问 main() 中 Space 对象的圈子成员时,我得到的唯一错误消息突然出现。在我看来,好像我已经在相关范围内声明了 theBoard,即作为 Board 类的数据成员。

我的 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;
}

【问题讨论】:

    标签: c++ pointers vector declaration sfml


    【解决方案1】:

    在您的 main() 函数中,GameBoardBoard *,而不是 Board。所以要访问成员,您需要使用-&gt; 而不是.。例如:

    GameBoard->theBoard.size()
    

    [有些人(我就是其中之一)喜欢用pptr 前缀来命名他们的指针变量,以明确表达这种恼怒。]

    【讨论】:

    • 哦,可爱的系统匈牙利符号...(我讨厌它!):) 我理解 p 前缀,但从那里请:不要。
    • @David:是的,这是我使用匈牙利语的极限。我认为p 很重要,因为指针对所需的基本语法有如此巨大的影响。我同意所有lpsz Microsoft 的东西都是疯狂的!
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      如果你仔细阅读,这个错误是相当明确的:

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

      第一行告诉您,您有一个指向Board 对象的指针,并且您正试图直接访问一个成员。那就是:

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

      另请注意,GameBoard.*theBoard[i].m_Circle 可能不是您想要的,您可能想要(我猜是因为缺少重要的部分)GameBoard-&gt;theBoard[i]-&gt;m_Circle 之类的东西。

      【讨论】:

        【解决方案4】:

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

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

        由于theBoard的元素也是指针,所以我在访问m_Circle时使用了指针表示法。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-07-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-05-29
          相关资源
          最近更新 更多