Static casting a function?

I have a problem in which i am trying to make my OpenGL Viewport to resize when the window is resized. But the problem is that i keep getting the error: A nonstatic member reference must be relative to a specific object. Please help!

Relevant code
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "hMsg.h"
using namespace EEngine;

hMsg::hMsg(OGLAbstr* GL)
{
	gl = GL;
	stop = true;
}

hMsg::~hMsg()
{
	
}

LRESULT CALLBACK hMsg::WndProcGame(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg)
	{
		case WM_DESTROY: PostQuitMessage(0); break;
		case WM_COMMAND: //app->regPress(hWnd, msg, wParam, lParam); 
			break;
		case WM_SIZE: //app->regSize(hWnd, msg, wParam, lParam); 
			break;
		case WM_RBUTTONUP: //app->regPopup(LOWORD(lParam), HIWORD(lParam)); 
			break;
	}
	return (DefWindowProc(hWnd, msg, wParam, lParam));
}

LRESULT CALLBACK hMsg::WndProcEditor(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg)
	{
		case WM_DESTROY: 
			PostQuitMessage(0); 
			break;
		case WM_COMMAND: 
			MenuProc(hWnd, msg, wParam, lParam);
			break;
		case WM_SIZE: resize(hWnd, msg, wParam, lParam);
			break;
		case WM_RBUTTONUP: //app->regPopup(LOWORD(lParam), HIWORD(lParam)); 
			break;
		default:
			DefWindowProc(hWnd, msg, wParam, lParam);
			break;
	}
	return (DefWindowProc(hWnd, msg, wParam, lParam));
}

LRESULT CALLBACK hMsg::MenuProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (lParam)
	{
		//default:
			//DefWindowProc(hWnd, msg, wParam, lParam);
	}
	switch (wParam)
	{
		case ID_FILE_EXIT:
			PostQuitMessage(0);
			break;
		default:
			DefWindowProc(hWnd, msg, wParam, lParam);
	}
	return (DefWindowProc(hWnd, msg, wParam, lParam));

}


void hMsg::pumpMessage()
{
	if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
	{
		if(msg.message == WM_QUIT)
		{
			stop = false;
		}
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
}

LRESULT CALLBACK hMsg::resize(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	resizeView(); //Here
	return(0);
}

void hMsg::resizeView()
{
#ifdef _GAME
	RECT rRect; GetClientRect(gl->Window, &rRect);
#endif
#ifdef _EDITOR
	RECT rRect; GetClientRect(gl->RenderWindow, &rRect);
#endif
	glViewport(0, 0, rRect.right, rRect.bottom);
}
Last edited on
What variable/line is it complaining about?
You are probably trying to call a pointer to member function without an object.

This code demonstrate the usage of pointer to member functions:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class A
{
public:
void test() {}
};

int main()
{
void (A::*ptr)() = &A::test; // storing the member function into ptr variable;
A a; // declaring variable of type A
(a->*ptr)(); // call ptr with a
A* pa = &a; // declaring variable of type A*
(pa->*ptr)(); // call ptr with pa
}
I have marked the spot where i get the error. This is a part of a heriarky of classes, and i am providing this class with the required pointer. Here is the .h file:

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
#ifndef _HMSG_H_
#define _HMSG_H_

#include <Windows.h>
#include "resource.h"
#include "OGLAbstr.h"
namespace EEngine
{
class hMsg
{
public:
	hMsg(OGLAbstr* GL);
	~hMsg();
	static LRESULT CALLBACK WndProcGame(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
	static LRESULT CALLBACK WndProcEditor(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
	static LRESULT CALLBACK MenuProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
	void pumpMessage();

	void resizeView();

	static LRESULT CALLBACK resize(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

	bool stop;
private:
	MSG msg;
	OGLAbstr* gl;
};
}

#endif 
In resize() you need to call the resizeView() function on a hMsg object.
But it is a member of the same class as resize()?!
resize() is a static member function. The call does not belong to any hMsg object. It's just like calling a non-member function. There is no why it could know on what object you want to call resizeView().
Thanks! It solved the problem to create a global object and then using it to call the function!
Topic archived. No new replies allowed.