[英]C++ using a member function from from one class in the main function?
I have my main and im setting up input but im using getters/setters too use a method from InputHander to my main but doing so is causing a stack overflow as there is just an infinite loop going on. 我有我的main和im设置输入,但是我使用getters / setters也使用从InputHander到我的main的方法,但是这样做会导致堆栈溢出,因为只有无限循环正在进行。
The warning i get: 我得到的警告:
warning C4717: 'key_callback' : recursive on all control paths, function will cause runtime stack overflow
I have no other idea how too call this method other than use a getter and setter 除了使用getter和setter之外,我没有其他想法可以调用此方法
Main.cpp 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);
}
InputHandler.h InputHandler.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);
};
InputHandler.cpp InputHandler.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;
}
}
I think you meant to write 我想你打算写
/* Gives keys events */
static void InputHandler::key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
and 和
/*
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 InputHandler::key_callback(window, key, scancode, action, mods);
}
However you need to pass some InputHandler instance somewhere, otherwise the use of a class is rather pointless. 但是,您需要在某个地方传递一些InputHandler实例,否则使用类是毫无意义的。
As the error message says, this function: 如错误消息所述,此函数:
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
return key_callback(window, key, scancode, action, mods);
}
calls itself, entering a recursive death spiral. 自称,进入了递归死亡螺旋。 Presumably, you meant 想必你是说
return InputHandler::key_callback(window, key, scancode, action, mods);
More simply, remove the non-member function and set the real callback directly: 更简单地,删除非成员函数并直接设置实际回调:
glfwSetKeyCallback(window, &InputHandler::key_callback);
You'll also need to fix the definition of the static member: 您还需要修复静态成员的定义:
/*no static here*/
void InputHandler::key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{// ^^^^^^^^^^^^^^
// whatever
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.