• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 370
  • Last Modified:

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?

0
oxygen_728
Asked:
oxygen_728
  • 6
  • 5
  • 2
  • +1
8 Solutions
 
Infinity08Commented:
>> I thought a static variable was limited in scope to the file it was declared in.
correction : use "compilation unit" instead of "file" in the above !

So, you declare the static variables twice in the same compilation unit !!

Explanation : the pre-processor includes the header files, which results in this file1.cpp file :

    //start headers.h
    // ...
    //start somefile.h
    // ...
    static int V1;
    static int V2;
    // ...
    //end somefile.h
    // ...
    //end headers.h
    static int V1;
    static int V2;
    // ...

And then the compiler kicks in, seeing that in file1.cpp, V1 and V2 are declared twice !
0
 
oxygen_728Author Commented:

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
0
 
Infinity08Commented:
Of course, but you just have to change your "file" concept to the state AFTER the preprocessor, so AFTER all #include statements have been evaluated.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
oxygen_728Author Commented:
I'm a little confused about your previous comment Infinity, could you please elaborate some?
0
 
dennis_georgeCommented:
Each "cpp" file forms a compilation unit. As u might know that when u "#include" any header file into a cpp then that header file becomes the part the "cpp" file. This all is done by the preprocessor.... the resulting "cpp" file is the actual compilation unit....

so if you have two header files and a cpp file... such as
headers.h  #includes "somefile.h"

file1.cpp   #includes  "headers.h"

so in file1.cpp u will have both headers.h and somefile.h.... and the resulting "cpp" file have two instances of ur static variables.....



0
 
dennis_georgeCommented:
The illustration shown by Infinity08 shows the structure of the "cpp" file after the inclusion of the header files.....
[quote]
    //start headers.h
    // ...
    //start somefile.h
    // ...
    static int V1;
    static int V2;
    // ...
    //end somefile.h
    // ...
    //end headers.h
    static int V1;
    static int V2;
    // ...
[/quote]
0
 
Infinity08Commented:
>> I'm a little confused about your previous comment Infinity, could you please elaborate some?
Does dennis_george's ewplanation clear things up for you, or do you have further questions/problems ?
0
 
e_tadeuCommented:
Do this:

// somefile.h:

extern int V1;
extern int V2;

// Any other header:

extern int V1;
extern int V2;

// file1.cpp:

int V1; // declare external to any function and class
int V2; // but only in one file


The extern declarations only say this: this V1 and V2 variables are declared somewhere else, go find them.
So, they find them in file1.cpp (actually it is file1.o or .obj), and they all use this same instance.
0
 
oxygen_728Author Commented:
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!
0
 
e_tadeuCommented:
Oxygen, that is the right way to define a global variable in a header!
Just don't use the static keyword in a header! Only in cpp's!
0
 
Infinity08Commented:
>> By externing V1 and V2 in two different headers, this accomplishes something but I'm still not understanding it.
The static variables as you defined them are only visible in the "file1" compilation unit ... to access them from another compilation unit ("file2" for example), you need to add the extern lines as mentioned by e_tadeu in the "file2" compilation unit (either by putting it in the "file2.cpp" file, or in one of the headers that are included in the "file2.cpp" file).
0
 
oxygen_728Author Commented:
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 =)
0
 
Infinity08Commented:
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 !
0
 
oxygen_728Author Commented:
Thanks for the extra information Infinity, I appreciate your help
0
 
Infinity08Commented:
If you have any further questions about this, please ask them in this thread, and I'll be glad to help you with them !!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 6
  • 5
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now