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 2 11091
"ik" <ik****@gmail.com> wrote in message
news:10**********************@k17g2000odb.googlegr 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
"ik" <ik****@gmail.com> 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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....
|
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...
|
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...
|
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...
|
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...
| |
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,...
|
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;
|
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...
|
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 -...
|
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,...
|
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...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
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...
|
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,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
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...
|
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...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |