473,769 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Initializing a static variable inside a namespace

ik
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 "myNameSpac e.h"

myNameSpace::iM yInt = 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::iM yInt'
D:/users/others/cpp_trials/SimplePrograms/myNameSpace.h:5 : `int
myNameSpace::iM yInt' previously defined here

at the same time ..

#include "myNameSpac e.h"

int main(int argc, char* argv[])
{
myNameSpace::iM yInt = 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

Jul 22 '05 #1
2 11117

"ik" <ik****@gmail.c om> wrote in message
news:10******** **************@ k17g2000odb.goo glegroups.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 "myNameSpac e.h"

myNameSpace::iM yInt = 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::iM yInt'
D:/users/others/cpp_trials/SimplePrograms/myNameSpace.h:5 : `int
myNameSpace::iM yInt' previously defined here

at the same time ..

#include "myNameSpac e.h"

int main(int argc, char* argv[])
{
myNameSpace::iM yInt = 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 "myNameSpac e.h"

namespace myNameSpace {
int iMyInt = 0;
}

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

john
Jul 22 '05 #2

"ik" <ik****@gmail.c om> 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 "myNameSpac e.h"

myNameSpace::iM yInt = 0;
int myNameSpace::iM yInt = 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::iM yInt'
This is becuase you are redefining myNameSpace::iM yInt.
D:/users/others/cpp_trials/SimplePrograms/myNameSpace.h:5 : `int
myNameSpace::iM yInt' previously defined here

at the same time ..

#include "myNameSpac e.h"

int main(int argc, char* argv[])
{
myNameSpace::iM yInt = 0;
Inside a function this is just an expression assigning 0 to
myNameSpace::iM yInt.
return 0;
}


Sharad
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
3670
by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc. And i have things to offer, and to request. And a lot of ideas, but who needs them.... here's an example (from type_struct.py):
6
3164
by: pembed2003 | last post by:
Hi all, I am reading the book "C++ How to Program" and in the chapter where it discuss scope rule, it says there are four scopes for a variable: function scope file scope block scope function-prototype scope I think(might be wrong):
2
3590
by: Drew McCormack | last post by:
I am getting an error in g++ 4.0.0 that I did not get in g++ 3.4. I have a header with the following const variables with namespace scope: namespace Periphery { extern const double ProtonMassInAtomicUnits = 1836.152755656068; } I try to use these in another header to initialize static const member
9
6367
by: Bryan Parkoff | last post by:
I have noticed that C programmers put static keyword beside global variable and global functions in C source codes. I believe that it is not necessary and it is not the practice in C++. Static keyword is useful inside struct, class, and function only unless you want to force local variable to be global variable so static is used. Do you have idea why most programmers do this? Bryan Parkoff
1
3536
by: Sandro Bosio | last post by:
Hello everybody, my first message on this forum. I tried to solve my issue by reading other similar posts, but I didn't succeed. And forgive me if this mail is so long. I'm trying to achieve the following (with incomplete succes): I want in a given namespace Parameters a list of "initializers" (which are objects derived from a simple interface that can be implemented anywhere, and are used to define which parameters the program will take at...
8
7519
by: SM | last post by:
I've always wonder if there is diference when declaring and initializing a varible inside/outside a loop. What's a better practice? Declaring and initializing variables inside a loop routine, like this: for(var i=0; i<list; i++) { var name = list; }
3
5790
by: StephQ | last post by:
In utility.hpp I have: namespace utility { template <class T, double (T::*F)(double) const> class Display { private: static double resolution;
14
6025
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared static inside functions (i.e. local static objects) 5. objects declared at file scope.
16
8624
by: RB | last post by:
Hi clever people :-) I've noticed a lot of people stating not to use static variables with ASP.NET, and, as I understand it, the reason is because the variable is shared across user sessions - which is Very Bad (tm) for reasons I understand! However, does this rule apply only to global static variables, or does it apply to procedure-level static variables.
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10216
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8873
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.