gpt4 book ai didi

c++ - 事件对象不采用 C++ 类

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:48:55 25 4
gpt4 key购买 nike

我正在使用 C++ 游戏教程,我无法弄清楚为什么我创建的类对象会给我“表达式必须具有类类型”的错误。该对象称为“menuEvent”,当您将鼠标悬停在变量上时,在摘要的第一行显示“sf::Event menuEvent”,但在摘要的第二行显示“Error:expression must have class type ”。它在同一个简报中自相矛盾,我无法弄清楚。我正在使用 C++ 代码在 Visual Studio 2015 中工作。任何帮助将不胜感激。这是我得到的;

这篇文章来 self 的一个外部依赖文档;

namespace sf
{
class Event
{
public:
struct MouseButtonEvent
{
Mouse::Button button; ///< Code of the button that has been pressed
int x; ///< X position of the mouse pointer, relative to the left of the owner window
int y; ///< Y position of the mouse pointer, relative to the top of the owner window
};
};
}

那么这一段来 self 的一个源文件;

MainMenu::MenuResult MainMenu::HandleClick(int x, int y)
{
std::list<MenuItem>::iterator it;

for (it = _menuItems.begin(); it != _menuItems.end(); it++)
{
sf::Rect<int> menuItemRect = (*it).rect;

if(menuItemRect.contains(sf::Vector2<int>(x,y)))
{
return it->action;
}
}

return Nothing;
}

MainMenu::MenuResult MainMenu::GetMenuResponse(sf::RenderWindow& window)
{
sf::Event menuEvent;

while (true)
{
window.pollEvent(menuEvent);
// The above line with menuEvent it reads as being of the sf::Event type

if (menuEvent.type == sf::Event::MouseButtonPressed)
{
//But the below line here when I hover over "menuEvent" it shows that it's of the sf::Event type, but then right below that it says "Error:expression must have class type".
return HandleClick(menuEvent.MouseButtonPressed.x , menuEvent.MouseButtonPressed.y);
}

if (menuEvent.type == sf::Event::Closed)
{
return Exit;
}
}

最佳答案

您声明了一个名为 sf::Event 的类。

此类声明了一个名为 MouseButtonEvent 的内部类。

MouseButtonEvent 不是类成员。这是一个内部类。

您声明了一个名为“menuEvent”的 sf::Event 实例:

sf::Event menuEvent;

您的编译器无法编译以下表达式:

HandleClick(menuEvent.MouseButtonPressed.x,
menuEvent.MouseButtonPressed.y);

错误是因为 sf::Event 没有名为 MouseButtonPressed 的类成员(从技术上讲这不完全正确,但我注意到这个问题没有一个“语言律师”标签,所以我玩得很快...)

表达式“menuEvent.MouseButtonPressed”是对menuEvent 类“MouseButtonPressed”的成员的引用。

没有这样的类成员。 MouseButtonPressed 是内部类,而不是类成员。

我不清楚你的代码的意图,所以我无法建议什么是代码在这里试图完成的任何正确语法。

关于c++ - 事件对象不采用 C++ 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32928860/

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