Class

So I work these days with sdl, and I wanted to make a little easy my work, so I started to make with class. The real problem showed up when I declared window

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Display::Display(string& title, int width, int height)
{
	SDL_Init(SDL_INIT_EVERYTHING);

	SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);


	window = SDL_CreateWindow(title.c_str() ,SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL);
	if (!window)
	{
		cout << "Error(SDL): " << SDL_GetError();
	}
	GLcontext = SDL_GL_CreateContext(window);

	if (!GLcontext)
	{
		cout << "Error(SDL): " << SDL_GetError();
	}

	GLenum err = glewInit();
	if (err != GLEW_OK)
	{
		cout << "Error(glew): " << glewGetErrorString(err);
		running = false;
	}
}  

so from this constructor, I get errors like C2597 illegal reference to non-static member "Display::window" - for all window instances and "Display::GLContext" and "Display::running"

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <string.h>

class Display
{
public:
	Display(string& title, int width, int height);
	~Display();
	void Run();
	bool running = true;
private:
	SDL_Window* window;
	SDL_GLContext GLcontext;
};

thats my header, and every non-functions/non-methods get this error every time it's used
Did you try making the private SDL_Window* window; a static member?
static SDL_Window* window;


If that works you can probably guess what I'll ask about the GLContext...

As far as I understand it, SDL_Init(SDL_INIT_EVERYTHING); should only be called once, I don't know if an error occurs if it's called more than once, I'm not near a computer with SDL installed at the moment.
If so you might be able to wrap it in an #ifndef macro like you would a header file.
Anyone know if that would do the trick inside a class function?
Last edited on
I don't want to use static, because I want every object created to have it's own
Can someone say me what just happend?

I moved "#include "Display.h" under "using namespace std;" what?????
Topic archived. No new replies allowed.