【问题标题】:incomplete type GLFWwindow when wrapping a C++ class包装 C++ 类时不完整的类型 GLFWwindow
【发布时间】:2020-07-01 04:43:25
【问题描述】:

我正在尝试使用 Boost::Python 从 Python 中的 Application C++ 基类派生,并且正在努力包装 GLFW 回调,特别是由于 GLFWwindow* window 参数。

这是 MCVE:

#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>

#include <boost/python.hpp>

class Application
{
public:
    Application();
    GLFWwindow* window;
    virtual int onKeyPressed(GLFWwindow*, int, int, int , int);
};

Application *app;

Application::Application()
{
    glfwInit();
    window = glfwCreateWindow(800.0, 600.0, "example", NULL, NULL);
    glfwMakeContextCurrent(window);
}

int Application::onKeyPressed(GLFWwindow *window, int key, int scancode, int action, int mods)
{
    return 1;
}

struct ApplicationWrap : Application, boost::python::wrapper<Application>
{
    int onKeyPressed(GLFWwindow *window, int key, int scancode, int action, int mods)
    {
        return this->get_override("onKeyPressed")(window, key, scancode, action, mods);
    }
};

static void _onKeyPressed(GLFWwindow *window, int key, int scancode, int action, int mods)
{
    app->onKeyPressed(window, key, scancode, action, mods);
}


int main() {
    // app = new Application();
    // glfwSetKeyCallback(app->window, _onKeyPressed);
    // delete app;
    return 0;
}


BOOST_PYTHON_MODULE(example)
{
    namespace python = boost::python;

    python::class_<ApplicationWrap, boost::noncopyable>("Application")
    //.def("onKeyPressed", _onKeyPressed).staticmethod("_onKeyPressed")
    ;
}

// import example
// class DerivedApplication(example.Application):
//     def __init__(self):
//         example.Application.__init__(self)
//     def onKeyPressed(window):
//         print("successfully overrides example.Application.onKeyPressed.")

// DerivedApplication()

编译:

g++ -I/usr/include/python3.7 main.cpp -lglfw -lpython3.7 -lboost_python3

错误:不完整类型“struct GLFWwindow”的使用无效 类型ID(T) ^~~~~~~~~

注意:'struct GLFWwindow' typedef struct 的前向声明 GLFW窗口 GLFW窗口;

感谢您提供有关如何解决此问题的任何反馈。

【问题讨论】:

  • 错误指向哪一行代码?
  • 嗨,它发生在int onKeyPressed(...)ApplicationWrap。我正在尝试编写一个更详尽的示例,因为我刚刚意识到 onKeyPressed 需要是静态的才能传递给 glfwSetKeyCallback
  • 尝试BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID(GLFWwindow)将其注册为opaque pointer
  • @dxiv 解决了它:) 我将不得不阅读它是如何工作的。如果你想要代表,你把它变成一个答案。再次感谢!
  • 很高兴它有帮助。而且,是的,请查看并仔细检查,因为我上次玩这些已经有一段时间了,我可能还需要学习更新的技巧。

标签: c++ boost-python


【解决方案1】:

错误来自template &lt;class T&gt; inline type_info type_id() via...

template <class T>
struct registered_pointee
    : registered<
        typename boost::python::detail::remove_pointer<  // <-- fails for incomplete types
           typename boost::python::detail::remove_cv<
              typename boost::python::detail::remove_reference<T>::type
           >::type
        >::type
    >
{
};

...最初来自pointer_deep_arg_to_python&lt;Ptr&gt;::pointer_deep_arg_to_python(Ptr x)

要将GLFWwindow * 注册为opaque pointer,请在#include 之后插入以下内容。

BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID(GLFWwindow)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-06
    • 2016-12-07
    • 2017-01-08
    • 2012-11-18
    • 1970-01-01
    • 2013-09-15
    相关资源
    最近更新 更多