Advertisement

illegal function

Started by March 31, 2004 12:47 PM
13 comments, last by Izarf 20 years, 11 months ago
Some problems in lesson 1 keeps to be unsolved. The two funktions WndProc() and WinMain() is according to Visual c++ 6.0 illegal. what causes this kinds of problem? I have spent many hours in searching for the missing part without result. Please help me as soon as possible. Thank you!
define "illegal". are you getting compiler errors, linker errors? if so what _exactly_ are the errors you are getting? (i.e. copy and paste them to us)

-me
Advertisement
My debug logg is:

--------------------Configuration: GLWindow - Win32 Debug--------------------
Compiling...
GLWindow.cpp
...\GLWindow.cpp(312) : error C2601: ''WndProc'' : local function definitions are illegal
...\GLWindow.cpp(394) : error C2601: ''WinMain'' : local function definitions are illegal
Error executing cl.exe.

GLWindow.exe - 2 error(s), 0 warning(s)
The only time I''ve seen those errors are when you try to define a function within a function, such as
int LegalFunction(){  bool IllegalFunction(bool Parameter)  {    return !Parameter;  }  return 0;} 
That code would cause a problem on the third line, because that is a local function definition, and is not allowed.


int Agony() { return *((int*)0); } Mwahaha... >8)
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
it sounds like you accidentally have WndProc and WinMain inside of another function. did you edit the code at all before you tried to compile it? either way could you post your code (please put it between [ source ] [ /source ] tags [without the spaces] so that it doesn''t look like crap when you post it)

-me
Thank you!
I think that will help.
I will now take a closer look in the code and finally get this problem solved.

// Izarf
Advertisement
Now I''ve spent some time with the code.. One of the errors is fixed
(added } before WinMain())..
Still is does the WinProc() - problem remain, though. When i tried to do the same thing I set a big end in the middle of the code, so the compilator wondered where the header files were..
Here is the source, you''ve asked for:
#include <windows.h>   //windows headerfile#include <gl\gl.h>     //OpenGL32 Libary#include <gl\glu.h>    //GLu32 Libary#include <gl\glaux.h>  // GLaux LibaryHGLRC       hRC=NULL;HDC         hDC=NULL;HWND        hWnd=NULL;HINSTANCE   hInstance;bool keys[256];    // Array - used for the keyboard routinebool active=TRUE;    // Windows active flag set by defultbool fullscreen=TRUE;    // Fullscreen flag set to fullscreen mode by defultLRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);  //Declaration for WinProcGLvoid ReSizeGLScene(GLsizei width, GLsizei height)		  //Rezise, initialize the GL-window{	if (height==0) 				// Prevent a divide by zero by	{		height=1;				}					// Making height equl one	glViewport(0, 0, width, height);		// Reset the current viewportglMatrixMode(GL_PROJECTION);            // Select the projection matrixglLoadIdentity();			// Reset the projection matrix// Calculate the aspect ratio of the windowgluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);glMatrixMode(GL_MODELVIEW);		// Select the modelview matrixglLoadIdentity();			// Reset the modelview matrix}int InitGL(GLvoid)  			// All setup for OpenGL goes here{glShadeModel(GL_SMOOTH);  		// Enables smoothshadingglClearColor(0.0f, 0.0f, 0.0f, 0.0f);		//Black backgroundglClearDepth(1.0f);		// Depht buffer setupglEnable(GL_DEPTH_TEST);	// enables depht testingglDepthFunc(GL_LEQUAL);		// The type of depth test to doglHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); //Really nice perspective configs	return TRUE;  		//Initialization went ''OK''}//Here ends the very ground of the gl matrix..   the bases is above//If the code fail, the value will be "false" and it will quitint DrawGLScene(GLvoid)				//Here we do all drawing{	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the screen and the dephth buffer	glLoadIdentity();		//Reset the current modelview matrix	return TRUE;			//Everything went ok}GLvoid KillGLWindow(GLvoid)		//Propertly kill the window{	if (fullscreen)			//Are in fullscreen mode?{	ChangeDisplaySettings(NULL,0); //if so Swtitch back to normal desktop. The values as resolution, bitdepht and so on is stored in the registry. Through using "NULL,0" windows is forced to swtich back to normal.	ShowCursor(TRUE); 			// Show mouse pointer}	if (hRC)			//do we have a rendering Context{		if (!!wglMakeCurrent(NULL,NULL)){	MessageBox(NULL,"Release of DC and RC failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION); //Messagebox.. contents -> Title -> type of icon}if (!wglDeleteContext(hRC))		// Are we able to delete the rc?{MessageBox(NULL,"Release rendering context failed.","Shutdown error",MB_OK | MB_ICONINFORMATION);}		hRC=NULL;		// Set RC to NULL}if (hWnd && !DestroyWindow(hWnd))	// Are we able to set dc to null?	{	MessageBox(NULL,"Could not release hWnd.","Shutdown error",MB_OK | MB_ICONINFORMATION);	hWnd=NULL;	}if (!UnregisterClass("OpenGL",hInstance))	{	MessageBox(NULL,"Could not unregister class.","shutdown error",MB_OK | MB_ICONINFORMATION);	hInstance=NULL;		//set hInstance to NULL	}}//---------------------------------- End of error reporting//-----------Starting with OpenGL-windowBOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag){GLuint 		PixelFormat;  		//Holds the results after searching for a matchWNDCLASS	wc;		// windows class structureDWORD	dwExStyle;		//window extended styleDWORD	dwStyle;		//Window styleRECT WindowRect;		//Grabs Rectangle upper left/lower right valuesWindowRect.left=(long)0;		//set left value to 0WindowRect.right=(long)width;		//set right value to requested widthWindowRect.top=(long)0;			//set the top value to 0WindowRect.bottom=(long)height;		// set buttom value to requested heightfullscreen=fullscreenflag;		//set the global fullscreen flaghInstance		= GetModuleHandle(NULL);			// Grab An Instance For Our Window	wc.style		= CS_HREDRAW | CS_VREDRAW | CS_OWNDC;		// Redraw On Move, And Own DC For Window	wc.lpfnWndProc		= (WNDPROC) WndProc;				// WndProc Handles Messages	wc.cbClsExtra		= 0;						// No Extra Window Data	wc.cbWndExtra		= 0;						// No Extra Window Data	wc.hInstance		= hInstance;					// Set The Instance	wc.hIcon		= LoadIcon(NULL, IDI_WINLOGO);			// Load The Default Icon	wc.hCursor		= LoadCursor(NULL, IDC_ARROW);			// Load The Arrow Pointer	wc.hbrBackground	= NULL;						// No Background Required For GL	wc.lpszMenuName		= NULL;						// We Don''t Want A Menu	wc.lpszClassName	= "OpenGL";					// Set The Class Nameif (!RegisterClass(&wc)){	MessageBox(NULL,"Failed to register the window class.","Error",MB_OK|MB_ICONEXCLAMATION);	return FALSE;}if (fullscreen)		//Attemt fullscreen mode?{	DEVMODE dmScreenSettings;	//Device mode	memset(&dmScreenSettings,0,sizeof(dmScreenSettings));	//Makes sure the memor''s cleared	dmScreenSettings.dmSize=sizeof(dmScreenSettings);	// Size of the devmode structure	dmScreenSettings.dmPelsWidth = width;	//selected screen width	dmScreenSettings.dmPelsHeight = height;	//seltected screen height	dmScreenSettings.dmBitsPerPel = bits;	//selected bits per pixel	dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;//Above is the fullscreensettings//Try to set selected mode and get results. NOTE: CDS_FULLSCREEN gets rid of startbarif (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN) !=DISP_CHANGE_SUCCESSFUL){//if the mode fails, offer two options. quit or run in a windowif (MessageBox(NULL,"The requested fullscreen mode is not supported by\nYour video card. Use windowed mode instead?","NeHe GL",MB_YESNO|MB_ICONEXCLAMATION)==IDYES){	fullscreen=FALSE;	//Select windowed mode	}	else	{//Pop up a message box letting user know the program is closing.MessageBox(NULL,"Program will now close.","Error",MB_OK|MB_ICONSTOP);return FALSE;	//Exit (return false)}}}if (fullscreen)			// Are we still in fullscreen mode?{	dwExStyle=WS_EX_APPWINDOW;	// windows extended style	dwStyle=WS_POPUP;		//Windows style (no border around= perfect for fullscreen mode)	ShowCursor(FALSE);		// Hide mouse pointer}else{	dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;	//Windows extended style	dwStyle=WS_OVERLAPPEDWINDOW;			// Windos style (WS_OVERLAPPEDWINDOW creates a window with a title bar, sizing border, window menu, and minimize / maximize buttons)}AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwStyle);	//Adjust window to true request sizeif (!(hWnd=CreateWindowEx(					dwExStyle,	//extended style for the window		"OpenGL",	//Class name		title,		//Window title		WS_CLIPSIBLINGS |	//Required window style		WS_CLIPCHILDREN |	//REQUITED WINDOW STYLE		dwStyle, 		//Selected window style		0, 0,		//window position		WindowRect.right-WindowRect.left,	//calculate adjusted window width		WindowRect.bottom-WindowRect.top,	//Calculate adjusted window height		NULL,					//no parent window		NULL,		//No menu		hInstance, 	//Instance		NULL))){{	KillGLWindow();	MessageBox(NULL,"Window creation error.","ERROR",MB_OK|MB_ICONEXCLAMATION);	return FALSE;}static PIXELFORMATDESCRIPTOR pfd= 	// pfd tells windows how we want things to be{	sizeof(PIXELFORMATDESCRIPTOR),	// sIZE OF THIS PIXEL FORMAT DESCRIPTOR	1, 				// version number	PFD_DRAW_TO_WINDOW |		// fORMAT MUST SUPPORT WINDOW	PFD_SUPPORT_OPENGL |		// FORMAT MUST SUPPORT OpenGL	PFD_DOUBLEBUFFER,		// Must support double buffering	PFD_TYPE_RGBA,			// Reques an RBGA format	bits,				// select our color depth	0, 0, 0, 0, 0, 0,		// Volor bits are ignored	0,				// No alpha number	0,				// Shift bit ignored	0,				// No acumulation buffer	0, 0, 0, 0,			// Accumulation bits ignored	16,				// 16 nits z-buffer (depth buffer)	0,				// No stencil buffer	0,				// No auxiliary buffer	PFD_MAIN_PLANE,			// Main drawing layer	0,				// Reserved	0, 0, 0				// Layer masks ignored};if (!(hDC=GetDC(hWnd)))		// Did we get a device context{	KillGLWindow();		// Reset the display	MessageBox(NULL,"Can''t create a GL device context.","ERROR",MB_OK|MB_ICONEXCLAMATION);	return FALSE;}if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd)))		// Did windows find a matching pixel format?{	KillGLWindow();		//Reset the display	MessageBox(NULL,"Can''t fint a suitable pixoleformat.","ERROR",MB_OK|MB_ICONEXCLAMATION);	return FALSE;}if (!SetPixelFormat(hDC,PixelFormat,&pfd))	// Are we able to set the pixelformat?{	KillGLWindow();	MessageBox(NULL,"Can''t set the pixelformat.","ERROR",MB_OK|MB_ICONEXCLAMATION);	return FALSE;}if (!(hRC=wglCreateContext(hDC))){	KillGLWindow();	MessageBox(NULL,"Can''t Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);	return FALSE;}if (!wglMakeCurrent(hDC,hRC)){	KillGLWindow();	MessageBox(NULL,"Can''t activate the GL rendering context.","ERROR",MB_OK|MB_ICONEXCLAMATION);	return FALSE;}if (!wglMakeCurrent(hDC,hRC)){	KillGLWindow();	MessageBox(NULL,"Can''t activate the gl rendering context.","ERROR",MB_OK|MB_ICONEXCLAMATION);	return FALSE;}// Following will get the window in foreground so it is given more priority etz..	ShowWindow(hWnd,SW_SHOW);	// Show the window	SetForegroundWindow(hWnd);	// Slightly higher the priority	SetFocus(hWnd);			// Sets keyboard focus to the window	ReSizeGLScene(width, height);	// Set up our perspective GL screen//Finaly the InitGL()! In this function are things as textures and lightning placed. This is the GL it self.// Following code will stop the program if InitGL() has an error.if (!InitGL()){	KillGLWindow();		// Reset the display	MessageBox(NULL,"Initialization Failed","ERROR",MB_OK|MB_ICONEXCLAMATION);	return FALSE;}					return TRUE;	// Tell WinMain to keep the program running (Success!)}		LRESULT CALLBACK WndProc(		HWND hWnd, 		// Handle for this window	UINT uMsg,		// Message for this window	WPARAM wParam,		// Additional message information	LPARAM lPARAM)		// Additional message information{	switch (uMsg)								// Check For Windows Messages	{			case WM_ACTIVATE:						// Watch For Window Activate Message		{			if (!HIWORD(wParam))					// Check Minimization State			{				active=TRUE;					// Program Is Active			}			else			{				active=FALSE;					// Program Is No Longer Active			}			return 0;						// Return To The Message Loop		}					case WM_SYSCOMMAND:						// Intercept System Commands		{			switch (wParam)						// Check System Calls			{				case SC_SCREENSAVE:				// Screensaver Trying To Start?				case SC_MONITORPOWER:				// Monitor Trying To Enter Powersave?				return 0;					// Prevent From Happening			}			break;							// Exit		}					case WM_CLOSE:							// Did We Receive A Close Message?		{			PostQuitMessage(0);					// Send A Quit Message			return 0;						// Jump Back		}					case WM_KEYDOWN:						// Is A Key Being Held Down?		{			keys[wParam] = TRUE;					// If So, Mark It As TRUE			return 0;						// Jump Back		}						case WM_KEYUP:							// Has A Key Been Released?		{			keys[wParam] = FALSE;					// If So, Mark It As FALSE			return 0;						// Jump Back		}						case WM_SIZE:							// Resize The OpenGL Window		{			ReSizeGLScene(LOWORD(lParam),HIWORD(lParam));		// LoWord=Width, HiWord=Height			return 0;						// Jump Back		}	}					// Pass All Unhandled Messages To DefWindowProc	return DefWindowProc(hWnd,uMsg,wParam,lParam);}}int WINAPI WinMain(	HINSTANCE hInstance,		// Instance	HINSTANCE hPrevInstance,	// Previous instance	LPSTR	  lpCmdLine,		// Command line parameters	INT	  nCmdShow)		// Window show state{MSG msg;		// Windows message struktureBOOL done=FALSE;	// Bool bariable to exit loop  (As long as done is false the program keeps running)// Ask the user wich mode they preferif (MessageBox(NULL,"Would You Like To Run In Fullscreen Mode?","Start Fullscreen?",MB_YESNO|MB_ICONQUESTION)==IDNO){	fullscreen=FALSE;		//Windowed mode}// Create our OpenGL windowif (!CreateGLWindow("Izarf''s OpenGL Framework",640,480,16,fullscreen)){	return 0;		// Quit if window was not created}while(!done)		// Loop that runs until done=TRUE{	if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))	// Ist there a message waiting?	{		if (msg.message==WM_QUIT)	// Have we recived a quit message?		{			done=TRUE;		// If so done=TRUE		}		else				// If not, deal with windows messages		{			TranslateMessage(&msg);	// Translate message			DispatchMessage(&msg);	// Dispatch message		}	}	else				// If there are no messages	{	// Draw the OpenGL scene. Watch for ESC key adm quit messages.		if (active)		// Is the Program active?		{			if (keys[VK_ESCAPE])	// Was ESC pressed?			{				done=TRUE;	// ESC signaled a quit			}			else			// Not time to quit, update screen			{				DrawGLScene();	// Draw the scene				SwapBuffers(hDC);	// Swap the buffers (Double buffering)			}		}		if (keys[VK_F1])		// Is F1 being pressed?		{			keys[VK_F1]=FALSE;	// If so make key false			KillGLWindow();		// Kill our current window			fullscreen=!fullscreen;	// Toggle fullscreen / Windowed mode						// Create our OpenGL Window			if (!CreateGLWindow("Izarf''s OpenGL",640,480,16,fullscreen))			{				return 0;	// Quit if window was not created			}		}	}}// Shut downKillGLWindow();		// Kill the windowreturn (msg.wParam);	// Exit the program}
you're code is HORRIBLY formatted and that's your problem. you have an extra curly brace in a few places. go back through your code and use proper indentation and you'll find the errors in about 30 seconds.

this is what proper indentation looks like:

void function(){    if ()    {        if ()        {        }    }}

this is what your code looks like:
   void function(){    if (){    if (){}}}


see how much more clear it is the right way?

-me

[edited by - Palidine on March 31, 2004 2:53:09 PM]
Yes.. I noticed that in the end of my code.. That''s why I started to format more correct.

Now when we''re talking about the subject, Visual Studio 7 has a very good GUI to avoid these kind of errors.

Thank you for your assistance!
While we''re on the subject, is there any way to get VC++ to do "smart indenting" for you, i.e. you select all the text and it applies the relevant indentation (like that shown by palidine)? I have used a language called MATLAB before and its editor has this feature, which is really very useful. It seems to me that VC++''s definition of "smart indent" it to put tabs in after you press return (where relevant) but not to go through your whole listing and apply formatting.

This topic is closed to new replies.

Advertisement