-
Splitting up a file
Hello everyone. On the project im working on, I have a file which is getting rather large, full of functions, that I would like to split up into multiple files. Generally, this wouldn't be a problem at all, but I've run into something that I can't figure out. Each function in the one big file now uses one or more file streams for file I/O. When I put half of the functions into another file, I get the error that they couldn't find the objects (streams), because they were defines in the first file. If I put all the stream object delarations in a .h file, then include it to both of the now split up function files, it will give me redefinition errors. I've been trying lots of things, but can't figure it out. Anyone know how I can make it work? Thanks.
-
I've been having the same problem until today. I finally figured it out. What compiler are you using?
-
Im using MSVC++ 6 Standard.
-
Ok, I'm using VC++ 6.0 Enterprise, so the method should be the same, but I'm not sure if this is how you are supposed to do it, it was all guess and hope for no errors.
I had made a poker game, blackjack game, and a menu screen. I wanted to be able to call the blackjack main from the menu. I renamed main blackjackmain() and I made a function prototype for it. Then I added the function prototype for blackjack main in my menu file, and then I was able to call it. To make all the files in the same project, I added C++ source files and pasted my code from earlier projects.
This took me forever. It turns out that you don't need to use any include statements to link the files together, VC++ does it for you. If this doesn't work perfectly, just mess with it. good luck, hope this helps.
-
Hey, thanks for the tip, although im not sure if its what im looking for. Its the actual file streams that are causing the problem, as I declare them like:
ofstream blah(blh.txt);
but I dont know how to be able to use 'blah' in different functions that are in multiple files. I may have misunderstood your responce, so if I did, sorry about that ;). Thanks.
-
Declare blah in one file and declare extern blah in the others. I think. I'm not too sure, but maybe if someone that has more than 6 months of c++ expericence would step in, you would get a good answer. Sorry if I didn't explain it too well, I'm learning by trial and error.
-
The only thing I can think of is to resort your includes:
Code:
#include <iostream.h>
#include <fstream.h>
#include "headers_that_require_fstream.h"
or if you have blah as a global
Code:
#include <iostream.h>
#include <fstream.h>
ofstream blah;
#include "headers_that_require_blah.h"
or I could be wrong :)
-
K, well their global, so if I put the function declarations below the streams? Ill try it, thanks.