H
Hal Vaughan
I feel like I should know this or be able to find the answer, but I can't.
I've read a few tutorials or articles on namespaces, but this applies to
files and I don't see much on namespaces in regard to files.
When I first started working on my project (this is my "initiation" project
in C++), I had one .cpp file with definitions that were more or less global
(yes, I can hear the jaws dropping now). I was only using this while I was
working out a number of details and now everything is divided up and there
are no global variables.
While I was using this file of variables, I did
#include "varfile.cpp"
(Okay, *now* I know there's many problems with that, so I don't need to hear
it was wrong!)
When I did that I expected to have access in one source file to the
variables in that file. Now I've cleaned it up. I use some routines
across files, but I handle that with making a .h file for each source file
and including the function prototypes. No variables are used in any .h
files.
What has me confused is that I thought if I had a variable name in use in
one file, it was specific to that file and all the files had their own
namespace. That doesn't seem to be so. I'm using make and g++ to compile
my program, but even without make, if I compile the .o files for each
source file, when I try the final compilation, I get errors that some
variables are defined in two separate files.
I want to have variables that are used only in the file they're defined in,
but I also want to be able to use some of the functions in that same file
that use that variable. I thought everything in a file was in its own
namespace by default and other files would not know about it unless I put
it in an include file.
Obviously that's wrong.
So how do I define a variable in one source file that others won't see and
still be able to access the functions in that file?
Thanks!
Hal
I've read a few tutorials or articles on namespaces, but this applies to
files and I don't see much on namespaces in regard to files.
When I first started working on my project (this is my "initiation" project
in C++), I had one .cpp file with definitions that were more or less global
(yes, I can hear the jaws dropping now). I was only using this while I was
working out a number of details and now everything is divided up and there
are no global variables.
While I was using this file of variables, I did
#include "varfile.cpp"
(Okay, *now* I know there's many problems with that, so I don't need to hear
it was wrong!)
When I did that I expected to have access in one source file to the
variables in that file. Now I've cleaned it up. I use some routines
across files, but I handle that with making a .h file for each source file
and including the function prototypes. No variables are used in any .h
files.
What has me confused is that I thought if I had a variable name in use in
one file, it was specific to that file and all the files had their own
namespace. That doesn't seem to be so. I'm using make and g++ to compile
my program, but even without make, if I compile the .o files for each
source file, when I try the final compilation, I get errors that some
variables are defined in two separate files.
I want to have variables that are used only in the file they're defined in,
but I also want to be able to use some of the functions in that same file
that use that variable. I thought everything in a file was in its own
namespace by default and other files would not know about it unless I put
it in an include file.
Obviously that's wrong.
So how do I define a variable in one source file that others won't see and
still be able to access the functions in that file?
Thanks!
Hal