成员未在范围内声明?
因此,在读完一本介绍性书籍后,我正在尝试一些 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
I'm new to this (from had两年的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;
}
So I'm trying my hand at some C++ after finishing up an introductory book, and I've become stuck. I've made a vector of objects that each have an SFML circle object as a member, and I want main() to go and draw these circles. The vector is called theBoard
, but when I try to access it, I get the following error messages:
error: request for member 'theBoard' in 'GameBoard', which is of non-class type 'Board*'
error: 'theBoard' was not declared in this scope
I'm new to this (came from two years of Python), so I'm sure I made a mistake somewhere. Here is the relevant code for the board creation:
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() is a very, very long function that returns a vector of pointers to Space objects. I doubt there's a problem here, as the only error message I get crops up when I try to access the circle members of Space objects in main(). It seems to me as though I have declared theBoard
in the relevant scope, that is, as a data member of the Board class.
My main() function, in case it's important:
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在您的
main()
函数中,GameBoard
是一个Board *
,而不是Board
。因此,要访问成员,您需要使用->
而不是.
。例如:[有些人(我是其中之一)喜欢用前导
p
或ptr
前缀来命名他们的指针变量,以便使这种刺激明显明显。]In your
main()
function,GameBoard
is aBoard *
, not aBoard
. So to access members, you need to use->
instead of.
. e.g.:[Some people (I am one of them) like to name their pointer variables with a leading
p
orptr
prefix, in order to make this kind of irritation explicitly clear.]GameBoard 是一个指向 Board 对象的指针,因此您需要使用“->”运算符而不是“.”运算符访问其任何成员变量或方法。
GameBoard is a pointer to a Board object, and thus you need to use the "->" operator instead of the "." operator to access any of its member variables or methods.
如果您仔细阅读,该错误非常明显:
第一行告诉您有一个指向 Board 对象的指针,并且您正在尝试直接访问成员。也就是说:
另请注意,
GameBoard.*theBoard[i].m_Circle
可能不是您想要的,您可能想要(我猜测是因为缺少重要的位)类似GameBoard- >theBoard[i]->m_Circle
。The error is quite explicit if you read it carefully:
The first line is telling you that you have a pointer to a
Board
object and you are trying to access a member directly. That is:Also note that
GameBoard.*theBoard[i].m_Circle
might not be what you want, you probably want (I am guessing as there are important bits missing) something likeGameBoard->theBoard[i]->m_Circle
.GameBoard
是一个指针,所以语法应该是这样的:由于
theBoard
的元素也是指针,所以我在访问m_Circle
时使用了指针表示法。GameBoard
is a pointer, so the syntax should be this:Since elements of
theBoard
also are pointer, so I used the pointer notation when accessingm_Circle
.