2013-08-23 81 views
0

我有我的主要和即時設置輸入,但即時使用getters/setter也使用從InputHander我的主要方法,但這樣做會導致堆棧溢出,因爲只是一個無限循環進行。C++使用主函數中的一個類的成員函數?

警告我得到:

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); 
} 

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

#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; 
    } 
} 
+1

看看警告,然後看看'key_callback()'。你看到任何原因,函數不會無限遞歸(直到你離開調用堆棧空間,無論如何)?你看過警告了嗎?你有兩個'key_callbacks',並且你沒有轉發到正確的一個(並且不管第一個都不需要)。 – WhozCraig

+1

你不能使用「空白」,因爲「空白」從來不存在,永遠不會存在。 'void'是一個永久不完整的類型,所以它不能被實例化。你真的試圖使用「一個函數(返回無效)」嗎? –

+0

@BumblebeeStudio:你確實需要修正你的術語。你爲什麼稱呼函數「空洞」? (哪有這回事)。 * main *不是一個類,它是一個函數,特別是* main入口函數*。 * void *是C/C++不完整類型;沒有void變量,* void *被用作函數返回類型,表示該函數不返回任何內容。我強烈建議你在實際項目中使用該語言之前,先將你的C/C++知識打磨乾淨;否則你會遇到很多可避免的障礙。 – datenwolf

回答

3

隨着錯誤消息稱,這個功能:

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 
} 
+0

謝謝你們現在幫助了很多人 –

3

我想你的意思是寫

/* Gives keys events */ 
static void InputHandler::key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) 
{ 

/* 
    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); 
} 

但是你需要的地方通過一些InputHandler實例,否則使用類是毫無意義的。

相關問題