oxygen_728
asked on
static variables - Different files - Redefinition error?
Hi.
I declared a couple static variables V1 and V2 in file1.cpp.
I declared a couple static variables V1 and V2 in somefile.h
file1.cpp #includes "headers.h"
headers.h #includes "somefile.h"
I thought a static variable was limited in scope to the file it was declared in.
Any tips?
I declared a couple static variables V1 and V2 in file1.cpp.
I declared a couple static variables V1 and V2 in somefile.h
file1.cpp #includes "headers.h"
headers.h #includes "somefile.h"
I thought a static variable was limited in scope to the file it was declared in.
Any tips?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I'm a little confused about your previous comment Infinity, could you please elaborate some?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Dennis and Infinity - Yes, that makes perfect sense. Thank you very much.
e_tadeu - I'm not entirely clear about your prevoius comment.
By externing V1 and V2 in two different headers, this accomplishes something but I'm still not understanding it.
Thanks for your time!
e_tadeu - I'm not entirely clear about your prevoius comment.
By externing V1 and V2 in two different headers, this accomplishes something but I'm still not understanding it.
Thanks for your time!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ahh.. I miscommunicated.
I'd like to have a unique V1 and V2 usable inside different files. (one being a header, and the other being a file that includes the header).
It's not a big deal, I just figured there was an easy way to do it.
Now that I know more about compilation units, I'll code accordingly =)
I'd like to have a unique V1 and V2 usable inside different files. (one being a header, and the other being a file that includes the header).
It's not a big deal, I just figured there was an easy way to do it.
Now that I know more about compilation units, I'll code accordingly =)
And just to make sure you understand this completely : you can't treat a header as a compilation unit, as it will never be compiled. It will be included in one or more compilation units that will contain everything the header specifies.
So, if you want two different compilation units, each having their own set of V1,V2 variables, you do this :
---- file1.cpp ----
// ...
static int V1;
static int V2;
// ...
---- ----
---- file2.cpp ----
// ...
static int V1;
static int V2;
// ...
---- ----
If you want the V1,V2 from compilation unit "file1" to be used in "file2", then do this :
---- file1.cpp ----
// ...
static int V1;
static int V2;
// ...
---- ----
---- file1.h ----
// ...
extern int V1;
extern int V2;
// ...
---- ----
---- file2.cpp ----
// ...
#include "file1.h"
// ...
---- ----
Of course you can play around with header files, as long as you realise that they only get included in a cpp file, and never get compiled themselves !
So, if you want two different compilation units, each having their own set of V1,V2 variables, you do this :
---- file1.cpp ----
// ...
static int V1;
static int V2;
// ...
---- ----
---- file2.cpp ----
// ...
static int V1;
static int V2;
// ...
---- ----
If you want the V1,V2 from compilation unit "file1" to be used in "file2", then do this :
---- file1.cpp ----
// ...
static int V1;
static int V2;
// ...
---- ----
---- file1.h ----
// ...
extern int V1;
extern int V2;
// ...
---- ----
---- file2.cpp ----
// ...
#include "file1.h"
// ...
---- ----
Of course you can play around with header files, as long as you realise that they only get included in a cpp file, and never get compiled themselves !
ASKER
Thanks for the extra information Infinity, I appreciate your help
If you have any further questions about this, please ask them in this thread, and I'll be glad to help you with them !!
ASKER
Darn, I think in VB.net it's purely file-based.
So, would you happen to know if It'd be possible for me to do what I want to do ?
Same variable names, different files, same compilation unit, static ?
Thanks