Skip to main content
gamedev.net
🔒 Locked

Problems calling private variables

Started by methinks 7 replies 1.5k views
Original Post
methinks
methinks
In my program, I have a variable declared as private in one of my classes, but I can't seem to call it, even though the function I am calling it from is part of the same class.

//header

class cPlatform
{
private:
 bool finish;

 void setFinished();
};


//CPP

void cPlatform::setFinished()
{
  finish = true;
}

(No, setFinished is not the actual function, that would be pointless) The compiler spits out the following error: error C2597: illegal reference to non-static member 'cPlatform::finish' I guess I still don't understand OOP well enough, but my understanding was that private meant that it could be accessed from within the same class.
cignox1
cignox1
Does the compiler give the error with that very code? If not, post the code that is creating the problem...
Fruny
Fruny
Quote:
(No, setFinished is not the actual function, that would be pointless)


The "actual function" in your code has been declared static. Static member functions do not have an object to operate on. The non-static member variable finish belongs to an object. Therefore it cannot be manipulated from the "actual function" in your code.

Static member functions can only access other static members - unless explicitely passed an object.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
iMalc
iMalc
Your function declaration doesn't have static in front of it does it?
That would give you this error.
STLDude
STLDude
Without seeing your code I can only guss that your setFinished() methos is declared static like

class cPlatform{private: bool finish; static void setFinished();};


Also, if you use VC, then look up that error in the help file, which will also show some example what can couse that error.
Zahlman
Zahlman
Just as a heads-up, if you do ever have an error due to access level (protected/private), your error message will instead be along the lines of (location in code): "identifier" is (private|protected) (other location in code): within this context. At least, that's how GCC reports them.
simon10k
simon10k
Why is the member function private?
-----------------------------Language: C++API: Win32, DirectXCompiler: VC++ 2003
methinks
methinks
You were right, I forgot that I had declared the function static.
Thanks.

The static function is actually a WndProc function. Originally when I tried to compile, it gave me the following error:
error C2440: '=' : cannot convert from 'LRESULT (__stdcall cPlatform::* )(HWND,UINT,WPARAM,LPARAM)' to 'WNDPROC'

I think this is because of the extra information needed to Id the instance, but the static fixed all that.

If it helps any, here's the code for the function in question
LRESULT CALLBACK cPlatform::WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){	switch (message)	{	case WM_KEYUP:		{			switch(wParam)			{			case VK_ESCAPE:				finish = true;				break;			}			break;		}	case WM_DESTROY:		finish = true;		return 0;	};    return DefWindowProc(hwnd,message,wParam,lParam);}

The thought behind doing it this way is to improve portability.
But now I still have to figure out how to output a value from the function.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.