不接受c++类的事件对象
Event object not taking class C++
我正在使用一个c++游戏教程,我很难弄清楚为什么我创建的类对象会给我"表达式必须具有类类型"的错误。该对象被称为"menuEvent",当你将鼠标悬停在变量上时,在摘要的第一行它说"sf::Event menuEvent",但随后在摘要的第二行它说"错误:表达式必须具有类类型"。它在同一份简报中自相矛盾,我弄不明白。我在Visual Studio 2015工作与c++代码。任何帮助都将非常感激。这是我得到的;
这部分来自我的一个外部依赖文档;
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
};
};
}
那么这部分来自我的一个源文件;
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
是一个内部类,而不是类成员。
我不清楚你的代码的意图,所以我不能建议什么是正确的语法,无论代码在这里试图完成。
成功了!非常感谢你的帮助,Sam Varshavchik。
我的最终代码版本;
外部块;
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
};
MouseButtonEvent btnVariable;
}
}
源文件段;
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);
if (menuEvent.type == sf::Event::MouseButtonPressed)
{
return HandleClick(menuEvent.btnVariable.x , menuEvent.btnVariable.y);
}
if (menuEvent.type == sf::Event::Closed)
{
return Exit;
}
}
}
相关文章:
- 在事件过滤器之前发出对象的事件
- 对象销毁后的事件
- 如何在OpenGL中使用关键事件来转换对象
- 在另一个 QThread 上运行成员方法时,无法将事件发送到其他线程拥有的对象
- 是否可以在C++(17)中捕获某些对象属性的破坏性赋值事件
- 如何在主事件循环之前创建一些对象?
- QT:QT无法将事件发送到另一个线程拥有的对象 - 原因
- WXWIDGETS将鼠标事件连接到WXPAINTDC上的形状对象以删除
- 是否有任何可能的方法将事件(自定义)附加到在运行时创建的对象?[C 构建器]
- 将 ETW 文件对象与 DiskIO 事件相关联
- 是否有任何对象可以用作FIFO和弹出数据的事件
- C++使用 shared_ptr 安全地删除事件对象有效负载
- 将C 对象暴露于QML并收听更改事件
- 无法在另一个线程-QT中向对象发送已发布的事件
- Qt 事件过滤器未检测到对象名称
- Win32获取从窗口WNDProc发送事件的对象的HWND
- 如何设置并发::事件对象,仅当它现在未处于信号状态时
- 我可以在 QP(量子平台)活动对象中等待 Windows 事件(WaitForMultipleObjects)吗?
- 如何调试是否设置了 Windows 事件对象
- 不接受c++类的事件对象