Key_callback undeclared but defined in glfw program

Main.cpp


#include "Window.h"
using namespace std;


int main()
{
    using namespace sparky;
    using namespace graphics;
    Window window("Sparky", 800, 600);
    glClearColor(0.2f, 0.3f, 0.8f, 1.0f);
    GLuint vao;
    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);
    while (!window.closed())
    {


        window.clear();
        if (window.isKeyPressed(GLFW_KEY_A))
        {
            std::cout << "pressed" << std::endl;
        }
#if 1
        glBegin(GL_TRIANGLES);
        glVertex2f(-0.5f, -0.5f);
        glVertex2f(0.0f, 0.5f);
        glVertex2f(0.5f, -0.5f);
        glEnd();
#else
        glDrawArrays(GL_ARRAY_BUFFER, 0, 6);
#endif
        window.update();


    }


  
    return 0;
   
    
}



Window.cpp

#include "Window.h"

namespace sparky {
	namespace graphics {
		bool Window::m_Keys[MAX_KEYS];
		bool Window::m_MouseButtons[MAX_BUTTONS];
		void Window_Resize(GLFWwindow* window, int width, int height);
		double Window::mx;
		double Window::my;

		Window::Window(const char* title, int width, int height)
		{
			m_Title = title;
			m_Width = width;
			m_Height = height;
			if (!init())
				glfwTerminate();
	
			for (int i = 0; i < MAX_KEYS; i++)
			{
				m_Keys[i] = false;
			}
			for (int i = 0; i < MAX_BUTTONS; i++)
			{
				m_MouseButtons[i] = false;

			}
		
		}
		Window::~Window()
		{
			glfwTerminate();
		}
		bool Window::init()
		{

			if (!glfwInit())
			{
				std::cout << "Failed to initialize GLFW!" << std::endl;
				return false;
			}


			m_Window = glfwCreateWindow(m_Width, m_Height, m_Title, NULL, NULL);
			if (!m_Window)
			{
				
					
			


				
				std::cout << "failed to create window" << std::endl;
				return false;
			}
		
			GLenum err = glewInit();
			if (GLEW_OK != err)
			{
				/* Problem: glewInit failed, something is seriously wrong. */
				fprintf(stderr, "Error: %s\n", glewGetErrorString(err));

			}
			fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
			fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
			glfwMakeContextCurrent(m_Window);
			glfwSetWindowUserPointer(m_Window, this);

			void* glfwGetWindowUserPointer(GLFWwindow * m_Window);
			glfwSetWindowSizeCallback(m_Window, Window_Resize);
			glfwSetKeyCallback(m_Window, key_callback);


			return true;

		}
		bool Window::isKeyPressed(unsigned int keycode)
		{
			if (keycode >= MAX_KEYS)
				return false;
			return m_Keys[keycode];
		}
		void Window::clear() const
		{
			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	}

		void Window::update()  {
			glfwPollEvents();

	

			glfwSwapBuffers(m_Window);
			
		}
		bool Window::closed() const
		{
			return glfwWindowShouldClose(m_Window) == 1;
		}
		void Window_Resize(GLFWwindow* window, int width, int height)
		{
			glViewport(0, 0, width, height);
		}

		 void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
		{
		Window* win = (Window*) 	glfwGetWindowUserPointer(window);	
		win->m_Keys[key] = action != GLFW_RELEASE;
		
		 }

	}
}```
**Window.h**

#pragma once
#include
#include <GL/glew.h>
#include <GLFW/glfw3.h>

namespace sparky {
namespace graphics {
#define MAX_KEYS 1024
#define MAX_BUTTONS 32
class Window
{

	private:
	
		const char* m_Title;
		int m_Width, m_Height;
		GLFWwindow* m_Window;
		bool m_Closed;
		static bool m_Keys[MAX_KEYS];
		static bool m_MouseButtons[MAX_BUTTONS];
		static double mx, my;
	public:
		Window(const char* name, int width, int height);
		~Window();
		void clear() const;
		void update();
		bool closed() const;

		inline int getWidth() const { return m_Width; }
		inline int getHeight() const { return m_Height; }
		static bool isKeyPressed(unsigned int keycode);
	private:
		bool init();
	friend static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
	};
}

}```
this ether needs troubleshooting or a fix in the next release this is horrible

Are you getting error message for your code? What is exact output of compiler?
key_callback is custom function in your code, not really provided by glfw - if you see error related to it, you need to fix that in your code.

it just throughs the error undeclared identifier c2065 in the output

This error is not related to glfw at all. It is issue with your C++ code - it is doing something that is not valid in C++ language.

If you cannot provide exact error message and location then nobody will be able to help you.
Please post exact error message and location where it happens. Also post what compiler (gcc/msvc/clang) are you using.

well im using visual studio 2022 with latest c and c++ compilers
and yea that all i get inside the error and complier output noting more I Don’t if theres some other I should be checking but that all I can see but in window.cpp
were the set callback has:
glfwSetKeyCallback(m_Window, key_callback);
that links with

void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
Window* win = (Window*) glfwGetWindowUserPointer(window);
win->m_Keys[key] = action != GLFW_RELEASE;

	 } 

and what it links with in windows.h
with the keycallback functions that is used
in here:
friend static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
but in window.cpp
dont know if this has anthying to do with it:
glfwSetWindowUserPointer(m_Window, this);
other than that I dont know what other details to provide other than just tried using an older visual studio C++ and c compiler in the configuration setttings

Your key_callback function is defined below where it is used. Compiler should see it before it can use it. Put key_callback above Window::Window constructor - below double Window::my; definition.

If you want to keep key_callback definition and end of file, then you should have its declaration at top of file, so Window constructor san see and use it.

And again - this has nothing to do with glfw. It does not need new release to fix anything. It is just incorrect C++ code you have.

thank you that did the trick man forgot about that one

but theres now another problem Im having after i build and used the new glfw released yesterday it intializes glew correctly but cant use GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);

I suggest you use better place asking about opengl questions, not in glfw support.
For example, Khronos has section for basic OpenGL questions: OpenGL: Basic Coding - Khronos Forums