Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Initializing a static variable inside a namespace

Reply
Thread Tools

Initializing a static variable inside a namespace

 
 
ik
Guest
Posts: n/a
 
      09-22-2004
Hello All,
I am facing a problem as follows.

I have a header file called myNameSpace.h which as the following
contents.

//Header file .. myNameSpace.h

namespace myNameSpace {
static int iMyInt = 0;
};

And my source file, as follows.

// Source file myNameSpace.cpp

#include "myNameSpace.h"

myNameSpace::iMyInt = 0;

int main(int argc, char* argv[])
{
return 0;
}

This gives me a compiler error.. on MingW.

D:/users/others/cpp_trials/SimplePrograms/myNameSpace.cpp:5: ISO C++
forbids declaration of `iMyInt' with no type
D:/users/others/cpp_trials/SimplePrograms/myNameSpace.cpp:5:
redefinition
of `int myNameSpace::iMyInt'
D:/users/others/cpp_trials/SimplePrograms/myNameSpace.h:5: `int
myNameSpace::iMyInt' previously defined here

at the same time ..

#include "myNameSpace.h"

int main(int argc, char* argv[])
{
myNameSpace::iMyInt = 0;
return 0;
}

This compiles..

I understand it is true for any variable declaration, at the global
namespace. Once declared any other reinitalization, is taken as a
re-definition.
I would like to know what is the correct explanation to this ?
Any help will be appreciated.
Thanks in Advance
~Ik

 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      09-22-2004

"ik" <> wrote in message
news: oups.com...
> Hello All,
> I am facing a problem as follows.
>
> I have a header file called myNameSpace.h which as the following
> contents.
>
> //Header file .. myNameSpace.h
>
> namespace myNameSpace {
> static int iMyInt = 0;


This is dubious. If you include this header in more than one source file you
will get multiple variables. If you change the value of the variable in one
file you will not see the change in another file. Is that what you want? It
seems unlikely.

> };
>
> And my source file, as follows.
>
> // Source file myNameSpace.cpp
>
> #include "myNameSpace.h"
>
> myNameSpace::iMyInt = 0;


This is wrong, you have missed out the type (int).

>
> int main(int argc, char* argv[])
> {
> return 0;
> }
>
> This gives me a compiler error.. on MingW.
>
> D:/users/others/cpp_trials/SimplePrograms/myNameSpace.cpp:5: ISO C++
> forbids declaration of `iMyInt' with no type
> D:/users/others/cpp_trials/SimplePrograms/myNameSpace.cpp:5:
> redefinition
> of `int myNameSpace::iMyInt'
> D:/users/others/cpp_trials/SimplePrograms/myNameSpace.h:5: `int
> myNameSpace::iMyInt' previously defined here
>
> at the same time ..
>
> #include "myNameSpace.h"
>
> int main(int argc, char* argv[])
> {
> myNameSpace::iMyInt = 0;
> return 0;
> }
>
> This compiles..


Because it is an assignment not a declaration.

>
> I understand it is true for any variable declaration, at the global
> namespace. Once declared any other reinitalization, is taken as a
> re-definition.


I wouldn't know about that. I try to declare and initialise my variables
once only.

> I would like to know what is the correct explanation to this ?
> Any help will be appreciated.
> Thanks in Advance
> ~Ik


Here's what you should be doing (probably)

//Header file .. myNameSpace.h

namespace myNameSpace {
extern int iMyInt;
}

// Source file myNameSpace.cpp

#include "myNameSpace.h"

namespace myNameSpace {
int iMyInt = 0;
}

It's really just the same as global variables outside of a namespace.

john


 
Reply With Quote
 
 
 
 
Sharad Kala
Guest
Posts: n/a
 
      09-22-2004

"ik" <> wrote in message
> Hello All,
> I am facing a problem as follows.
>
> I have a header file called myNameSpace.h which as the following
> contents.
>
> //Header file .. myNameSpace.h
>
> namespace myNameSpace {
> static int iMyInt = 0;


> };


There should be no trailing semicolon after a namespace definition.

> And my source file, as follows.
>
> // Source file myNameSpace.cpp
>
> #include "myNameSpace.h"
>
> myNameSpace::iMyInt = 0;


int myNameSpace::iMyInt = 0; // Redefinition

If your intent was to give it a value then it should have been in a function
block. You can't have such floating expressions without a function body.

> int main(int argc, char* argv[])
> {
> return 0;
> }
>
> This gives me a compiler error.. on MingW.
>
> D:/users/others/cpp_trials/SimplePrograms/myNameSpace.cpp:5: ISO C++
> forbids declaration of `iMyInt' with no type


This is because you missed on the "int".

> D:/users/others/cpp_trials/SimplePrograms/myNameSpace.cpp:5:
> redefinition
> of `int myNameSpace::iMyInt'


This is becuase you are redefining myNameSpace::iMyInt.

> D:/users/others/cpp_trials/SimplePrograms/myNameSpace.h:5: `int
> myNameSpace::iMyInt' previously defined here
>
> at the same time ..
>
> #include "myNameSpace.h"
>
> int main(int argc, char* argv[])
> {
> myNameSpace::iMyInt = 0;


Inside a function this is just an expression assigning 0 to
myNameSpace::iMyInt.

> return 0;
> }
>


Sharad


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Initializing a global namespace object Stanimir Stamenkov Javascript 39 07-30-2010 08:15 PM
Good practice question: Declaring/Initializing variables inside or outside a loop ? SM Javascript 8 05-01-2007 04:38 AM
Initializing a static const member array of a class type Steven T. Hatton C++ 1 04-19-2004 06:42 AM
Initializing static variables in application_start? Adam Smith ASP .Net 0 04-15-2004 01:10 AM
Initializing static class member Avi Uziel C++ 14 09-26-2003 06:53 PM



Advertisments