gpt4 book ai didi

c++ - 成员未在范围内声明?

转载 作者:太空狗 更新时间:2023-10-29 19:39:46 25 4
gpt4 key购买 nike

因此,在读完一本介绍性书籍后,我正在尝试学习一些 C++,但我陷入了困境。我制作了一个对象 vector ,每个对象都有一个 SFML 圆对象作为成员,我希望 main() 去绘制这些圆。该 vector 称为 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 对象的指针 vector 。我怀疑这里有问题,因为当我尝试访问 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;
}

最佳答案

在您的 main() 函数中,GameBoard 是一个 Board *,而不是一个 Board。所以要访问成员,你需要使用->而不是.。例如:

GameBoard->theBoard.size()

[有些人(我就是其中之一)喜欢用前导的 pptr 前缀来命名他们的指针变量,为了使这种明显的刺激。]

关于c++ - 成员未在范围内声明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6334325/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com