Hi, I'm trying to split up my game (about 1300 lines) into header files, but I'm coming up with a problem whenever I try and put a function in a header file, when that function was modifying some variables that were defined before int main in the .cpp. For example:
int variable1 (0);
int variable2 (0);
If it only modified one variable then I could just pass that variable and the return it:
(return variable1 + 1;)
But I don't know how to make a function in a header file modify several pre-existing variables. In the actual program, the variables are dependant on each other and the modifying is a lot more complicated, so I'd rather not split it into several functions and run one at a time if there's another way.
Thanks in advance for any help!
If you want your function to modify more than one value and pass those modified values back to the calling code, then you need to look up how to pass variables by reference.
Thanks, that looks perfect. I kind of taught myself, so my knowledge of c++ is usually good enough, but I'll often miss out important details like that. Thanks again.
It sounds to me as though you'd benefit from actually working through some structured tutorials, or a proper textbook. C++ is a rich language, with a lot of features, so it's possible you've missed out on a lot of stuff.
I did buy a book (You Can Do It! by Francis Glassborow), and I read and went through approximately the first third (up to containers) which gave me enough to do pretty much everything I've come up against when trying to write a proper game. This was the first thing that I hadn't been able to do with what I'd read. I think I'll go back and do the next section (though it did start to go into a lot just on Francis' header files that he uses for graphcis, but I've learnt winbgim so that's where I stopped).