creating multiple cpp files

hello,
i am new to visual c++ 2010 but the code that i am writing is quite long (not compared to other programs) and i was wondering how i can neaten it up. at the moment i have a header file with all the variables and a .cpp that holds the code

i have tried to add in a second cpp file but when i add it none of the variables are there but if i add the header file it comes up with an error about making the variables twice.

anyone know how to solve this problem?
It's probably worth reconsidering the design. It sounds as though you have lots of global variables. That's generally not good practice.

A better approach is to pass just the specific variables which are needed as parameters to each function.
parameters?
is it
void function (int parameter)
{
}
?
Last edited on
Roughly like that. Maybe up to this point you don't have any other functions apart from main() ? Certainly if you're reaching the stage where it seems like it needs separating into more than one file, then putting some of the functions into a separate cpp file would be one way to go.

In that case, the header file would contain the function prototypes, and the cpp file the definitions of the function body.
Tutorial on functions:
http://www.cplusplus.com/doc/tutorial/functions/
http://www.cplusplus.com/doc/tutorial/functions2/

I realise this isn't directly addressing the original question, sorry about that.

thanks. i am using quite a few functions :)
on this program i am using SDL. i have tried to use parameters for this but it comes up with an error. i need to refresh the screen but i would like to do it in screen.cpp. i have a fuction refresh to do this but it come up withan error.

void refresh(SDL_Surface* source)
{
SDL_Flip( source );
}

it is declared at main.cpp but this is on screen.cpp. there is some sore of error with SDL_flip but i do not understand it. this is meant to run in the while (running) loop

EDIT:
the new error is with the function itself now. it says:
Edit and Continue : warning 2006 : Variable or function was removed. Undeleted and undetected references could still exist: refresh (c:\users\user1\documents\visual studio 2010\projects\vc++\games\SDL\SDL\debug\screen.obj)

EDIT 2:
the error has changed back to the original error with the SDL_flip:
First-chance exception at 0x68126030 in SDL.exe: 0xC0000005: Access violation reading location 0x00000004.
Unhandled exception at 0x68126030 in SDL.exe: 0xC0000005: Access violation reading location 0x00000004.
First-chance exception at 0x68126030 in SDL.exe: 0xC0000005: Access violation reading location 0x00000004.
Unhandled exception at 0x68126030 in SDL.exe: 0xC0000005: Access violation reading location 0x00000004.


Last edited on
Unfortunately I've not used SDL so I'm not familiar with the details here. It may be better to start a new thread for this question.
Make sure that you don't pass a null pointer to SDL_Flip.
?

to run it i got
refresh( screen );

if i didnt put screen would that make it a NULL pointer?
If you have not assigned a value to screen that means the pointer is either uninitialized or that it is a null pointer, depending on where you defined it. In the lazy foo tutorials they assign the return value of SDL_SetVideoMode to screen. You can also get the pointer to the screen surface by using SDL_GetVideoSurface.
http://www.libsdl.org/cgi/docwiki.cgi/SDL_GetVideoSurface
Last edited on
Topic archived. No new replies allowed.