C++ 在主函数中使用来自一个类的成员函数?

标签 c++ input 3d glfw

我有我的 main 和 im 设置输入但我使用 getters/setters 也使用从 InputHander 到我的 main 的方法但这样做会导致堆栈溢出,因为只有一个无限循环正在进行。

我得到的警告:

warning C4717: 'key_callback' : recursive on all control paths, function will cause runtime stack overflow

除了使用 getter 和 setter 之外,我不知道如何调用此方法

main.cpp

#include <glfw3.h>
#include <stdio.h>
#include <iostream>

#include "InputHandler.h"

using namespace std;

/* Begin Void prototyping */
void error_callback(int error, const char* description);
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);

int main(void)
{
    GLFWwindow* window;

    /* Initializes error call-backs */
    glfwSetErrorCallback(error_callback);

    /* Initialize the library */
    if (!glfwInit())
        return -1;

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL); // fullscreen glfwGetPrimaryMonitor() (first NULL)
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

    /* Makes OpenGL context current */
    glfwMakeContextCurrent(window);

    /* Make the window's context current */
    glfwMakeContextCurrent(window);

    /* Receives input events */
    glfwSetKeyCallback(window, key_callback);

    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
        /* Render here */
        float ratio;
        int width, height;

        glfwGetFramebufferSize(window, &width, &height);
        ratio = width / (float) height;

        glViewport(0, 0, width, height);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glEnable(GL_DEPTH_TEST);
        glDepthFunc(GL_LEQUAL);

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(-ratio, ratio, -1.f, 1.f, 1.f, -1.f);
        glMatrixMode(GL_MODELVIEW);

        glLoadIdentity();
        glRotatef((float) glfwGetTime() * 10.f, 0.f, 10.f, 0.f);

        /* Swap front and back buffers */
        glfwSwapBuffers(window); 

        /* Poll for and process events */
        glfwPollEvents();
    }

    glfwDestroyWindow(window);

    glfwTerminate();
    return 0;
}

/*
Calls back the program if a GLFW function fail and logs it
*/
void error_callback(int error, const char* description)
{
    fputs(description, stderr);
}

/*
    Gets InputHandler::key_callback returns key_callback too use in Main.cpp
*/
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
    return key_callback(window, key, scancode, action, mods);
}

输入处理器.h

#pragma once

#include <glfw3.h>
#include <iostream>

using namespace std;

class InputHandler
{
public:
    InputHandler(void);
    ~InputHandler(void);

    static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
};

输入处理器.cpp

#include "InputHandler.h"

GLFWwindow* window;

InputHandler::InputHandler(void)
{
}


InputHandler::~InputHandler(void)
{
}

/* Gives keys events */
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
    switch(key)
    {
    case GLFW_KEY_ESCAPE:
        glfwSetWindowShouldClose(window, GL_TRUE);
        break;
    case GLFW_KEY_W:
        cout << "W working" << endl;
        break;
    case GLFW_KEY_A:
        break;
    case GLFW_KEY_S:
        break;
    case GLFW_KEY_D:
        break;
    case GLFW_KEY_1:
        break;
    case GLFW_KEY_2:
        break;
    }
}

最佳答案

正如错误信息所说,这个函数:

void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
    return key_callback(window, key, scancode, action, mods);
}

调用自己,进入递归的死亡螺旋。据推测,你的意思是

return InputHandler::key_callback(window, key, scancode, action, mods);

更简单的,去掉非成员函数,直接设置真正的回调:

glfwSetKeyCallback(window, &InputHandler::key_callback);

您还需要修复静态成员的定义:

/*no static here*/ 
void InputHandler::key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) 
{//  ^^^^^^^^^^^^^^
     // whatever
}

关于C++ 在主函数中使用来自一个类的成员函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18407600/

相关文章:

c++ - 如何使用 cadetill 的 GMLib?

javascript - 屏蔽输入插件

javascript - 如何使用 Sprite 作为输入翻转?

jquery - 强制聚焦于一个元素,但有异常(exception)。 (jQuery)

c - 在 C 中通过引用传递的 3d 数组

c++ - 对象析构函数中 "string"成员是否需要调用析构函数?

c++ - 谷歌云 TPU 支持 Tensorflow C++ API

c++ - 旋转矩阵的 Y 旋转

java - 圆的 3D 旋转使边缘交叉两点

c++ - 基本的Windows API