Trying to brush up on my C++ but I'm having some link errors. Suggestions welcomed :p
Errors:
Error 1 error LNK2005: "void __cdecl chooserand(void)" (?chooserand@@YAXXZ) already defined in main.obj C:\Users\dfurball\Documents\Visual Studio 2010\Projects\day1\day1\state.obj
Error 2 error LNK2005: _main already defined in main.obj C:\Users\dfurball\Documents\Visual Studio 2010\Projects\day1\day1\state.obj
Error 3 error LNK2005: "char state" (?state@@3DA) already defined in main.obj C:\Users\dfurball\Documents\Visual Studio 2010\Projects\day1\day1\state.obj
Error 4 error LNK2005: "int choice" (?choice@@3HA) already defined in main.obj C:\Users\dfurball\Documents\Visual Studio 2010\Projects\day1\day1\state.obj
Error 5 error LNK1169: one or more multiply defined symbols found
C:\Users\dfurball\Documents\Visual Studio 2010\Projects\day1\Debug\day1.exe 1
The compiler takes state.cpp (which includes main.h) and main.cpp (which includes the exact same main.h) and turns them into two separate object files. The linker then has to link together these object files to make the final program.
Each of these object files contains a definition of the functions chooserand and main, and the variables state and choice.
When you have functions and variables in the same namespace with identical names, how can the linker tell them apart? It cannot, so it does not know which one to use, so it complains. This is why we do not put function defintions in header files. We only put function prototypes in header files. If the function defintions were in the cpp file, they would exist only in that cpp file, and thus only in one object file, so the linker would know which one to use.
Why do you have a function main in state.h? That makes no sense at all. Note that in C++ main returns an int. void main() is in conflict with the standard.