GLWrapper构造器参数不匹配错误排查求助
编译错误排查:GLWrapper构造器参数不匹配
编译时出现两个错误:
- E0289 无匹配的
GLWrapper::GLWrapper构造器实例匹配参数列表 - C2665
GLWrapper::GLWrapper:没有重载函数能转换所有参数类型
以下是相关代码:
main代码
int main(int argc, char* argv[]) { GLWrapper* glw = new GLWrapper(1024, 768, "Piano Hammer"); //specify dimensions of window plus title printControls(); //print the program controls in the console if (!ogl_LoadFunctions()) //if load fails { fprintf(stderr, "ogl_LoadFunctions() failed. Exiting\n"); //inform user of error return 0; } glw->setRenderer(display); //bind display within event loop glw->setKeyCallback(keyCallback); //bind display within event loop glw->setReshapeCallback(reshape); //bind reshape within event loop initialise(glw); //initialise the window glw->eventLoop(); //bind the event loop delete(glw); return 0; }
wrapper_glfw.h
#include <string> /* Inlcude GL_Load and GLFW */ #include <glload/gl_4_0.h> #include <glload/gl_load.h> #include <GLFW/glfw3.h> class GLWrapper { private: int width; int height; char *title; double fps; void(*error_callback)(int error, const char* description); void(*renderer)(); void(*reshape)(GLFWwindow* window, int w, int h); void(*keyCallBack)(GLFWwindow* window, int key, int scancode, int action, int mods); bool running; GLFWwindow* window; public: GLWrapper(int width, int height, char *title); ~GLWrapper(); void setFPS(double fps) { this->fps = fps; } /* Callback registering functions */ void setRenderer(void(*f)()); void setReshapeCallback(void(*f)(GLFWwindow* window, int w, int h)); void setKeyCallback(void(*f)(GLFWwindow* window, int key, int scancode, int action, int mods)); void setErrorCallback(void(*f)(int error, const char* description)); int eventLoop(); GLFWwindow* getWindow(); };
wrapper_glfw.cpp
#include "wrapper_glfw.h" /* Inlcude some standard headers */ #include <iostream> #include <fstream> #include <vector> /* Constructor for wrapper object */ GLWrapper::GLWrapper(int width, int height, char *title) { this->width = width; this->height = height; this->title = title; this->fps = 30; this->running = true; /* Initialise GLFW and exit if it fails */ if (!glfwInit()) { std::cout << "Failed to initialize GLFW." << std::endl; exit(EXIT_FAILURE); } setErrorCallback(error_callback); glfwWindowHint(GLFW_SAMPLES, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); #ifdef DEBUG glfwOpenWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE); #endif window = glfwCreateWindow(width, height, title, 0, 0); if (!window){ std::cout << "Could not open GLFW window." << std::endl; glfwTerminate(); exit(EXIT_FAILURE); } /* Obtain an OpenGL context and assign to the just opened GLFW window */ glfwMakeContextCurrent(window); /* Initialise GLLoad library. You must have obtained a current OpenGL */ if (!ogl_LoadFunctions()) { std::cerr << "oglLoadFunctions() failed. Exiting" << std::endl; glfwTerminate(); return; } glfwSetInputMode(window, GLFW_STICKY_KEYS, true); } /* Terminate GLFW on destruvtion of the wrapepr object */ GLWrapper::~GLWrapper() { glfwTerminate(); } /* An error callback function to output GLFW errors*/ static void error_callback(int error, const char* description) { fputs(description, stderr); } /* Returns the GLFW window handle, required to call GLFW functions outside this class */ GLFWwindow* GLWrapper::getWindow() { return window; } /* GLFW_Main function normally starts the windows system, calls any init routines and then starts the event loop which runs until the program ends */ int GLWrapper::eventLoop() { // Main loop while (!glfwWindowShouldClose(window)) { // Call function to draw your graphics renderer(); // Swap buffers glfwSwapBuffers(window); glfwPollEvents(); } glfwTerminate(); return 0; } /* Register an error callback function */ void GLWrapper::setErrorCallback(void(*func)(int error, const char* description)) { this->error_callback = func; glfwSetErrorCallback(func); } /* Register a display function that renders in the window */ void GLWrapper::setRenderer(void(*func)()) { this->renderer = func; } /* Register a callback that runs after the window gets resized */ void GLWrapper::setReshapeCallback(void(*func)(GLFWwindow* window, int w, int h)) { reshape = func; glfwSetFramebufferSizeCallback(window, reshape); } /* Register a callback to respond to keyboard events */ void GLWrapper::setKeyCallback(void(*func)(GLFWwindow* window, int key, int scancode, int action, int mods)) { keyCallBack = func; glfwSetKeyCallback(window, keyCallBack); }
错误原因
GLWrapper构造器的第三个参数声明为char* title,但你在main函数中传入的是字符串字面量"Piano Hammer"。在C++标准中,字符串字面量的类型是**const char***,编译器不允许将const char*隐式转换为非const的char*(这是类型安全的限制,防止修改只读的字符串字面量导致未定义行为),因此触发参数不匹配的错误。
解决方案
方案1(推荐):修改构造器参数为const类型
这是最安全、符合C++规范的做法:
- 在
wrapper_glfw.h中,将类成员变量和构造器参数的char *title改为const char* title:
// 类私有成员 const char *title; // 构造器声明 GLWrapper(int width, int height, const char *title);
- 在
wrapper_glfw.cpp中,同步修改构造器的定义:
GLWrapper::GLWrapper(int width, int height, const char *title) { this->width = width; this->height = height; this->title = title; // 其余代码不变 }
这样修改后,字符串字面量"Piano Hammer"可以直接匹配const char*类型,编译错误会消失。
方案2(不推荐):强制转换字符串字面量
如果你不想修改构造器代码,可以在main中用const_cast强制转换字符串字面量的类型,但这种方法存在风险(字符串字面量是只读的,若后续代码尝试修改title会导致未定义行为):
GLWrapper* glw = new GLWrapper(1024, 768, const_cast<char*>("Piano Hammer"));
内容的提问来源于stack exchange,提问作者justvv
相关产品推荐
相关产品推荐

