不接受c++类的事件对象

Event object not taking class C++

本文关键字:事件 对象 c++ 不接受      更新时间:2023-10-16

我正在使用一个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;
      }
    }
  }