R
rossum
Either I have found a puzzle, or I am just being stupid. Inside a
namespace I have a class with a static const double member. The
member is not an int so I cannot initialise it within the class, I
have to initialise it outside the class. The problem is that when I
tried putting the initialisation in the header file immediately after
the class my compiler (g++) rejected it, citing "multiple definition":
g++.exe alphaMain.o alpha.o -o "Alpha.exe" -L"C:/DEV-CPP/lib"
alpha.o(.text+0x0):alpha.cpp: multiple definition of
`foo::AlphaC::m_doubl'
alphaMain.o(.text+0x0):alphaMain.cpp: first defined here
Then I tried moving the initialisation outside the namespace, but
still in the header file. Same error. Finally I put the
initialisation at the end of the .cpp file, outside the namespace
again. That seemed to work and compiled with no errors.
Another aspect to the puzzle is that when I concatenate the three
files: alpha.h, alpha.cpp and alphaMain.cpp into a single file then it
compiles normally with the initialisation where I first put it, after
the class and inside the namespace.
I can't see that I am doing anything stupid, can another pair of eyes
see my error?
rossum
(Getting ready to say "Doh!" and bang my head on the keyboard.)
// alpha.h -----------------------
#ifndef ALPHA_H_INCLUDE
#define ALPHA_H_INCLUDE
namespace foo {
class AlphaC {
private:
static const double m_doubl;
int m_data;
public:
AlphaC() : m_data(0) {}
void increment();
}; // end class AlphaC
// const double AlphaC::m_doubl = 1.0; // This doesn't work,
// but it does work if the 3 files are combined into one.
} // end namespace foo
// const double foo::AlphaC::m_doubl = 1.0; // This doesn't work
#endif // ALPHA_H_INCLUDE
// alpha.cpp ---------------------
#include "alpha.h"
namespace foo {
void AlphaC::increment() {
++m_data;
} // end increment()
} // end namespace foo
const double foo::AlphaC::m_doubl = 1.0; // This works. Why?
// alphaMain.cpp -----------------
#include "alpha.h"
int main() {
foo::AlphaC my_alpha;
my_alpha.increment();
return 0;
} // end main()
namespace I have a class with a static const double member. The
member is not an int so I cannot initialise it within the class, I
have to initialise it outside the class. The problem is that when I
tried putting the initialisation in the header file immediately after
the class my compiler (g++) rejected it, citing "multiple definition":
g++.exe alphaMain.o alpha.o -o "Alpha.exe" -L"C:/DEV-CPP/lib"
alpha.o(.text+0x0):alpha.cpp: multiple definition of
`foo::AlphaC::m_doubl'
alphaMain.o(.text+0x0):alphaMain.cpp: first defined here
Then I tried moving the initialisation outside the namespace, but
still in the header file. Same error. Finally I put the
initialisation at the end of the .cpp file, outside the namespace
again. That seemed to work and compiled with no errors.
Another aspect to the puzzle is that when I concatenate the three
files: alpha.h, alpha.cpp and alphaMain.cpp into a single file then it
compiles normally with the initialisation where I first put it, after
the class and inside the namespace.
I can't see that I am doing anything stupid, can another pair of eyes
see my error?
rossum
(Getting ready to say "Doh!" and bang my head on the keyboard.)
// alpha.h -----------------------
#ifndef ALPHA_H_INCLUDE
#define ALPHA_H_INCLUDE
namespace foo {
class AlphaC {
private:
static const double m_doubl;
int m_data;
public:
AlphaC() : m_data(0) {}
void increment();
}; // end class AlphaC
// const double AlphaC::m_doubl = 1.0; // This doesn't work,
// but it does work if the 3 files are combined into one.
} // end namespace foo
// const double foo::AlphaC::m_doubl = 1.0; // This doesn't work
#endif // ALPHA_H_INCLUDE
// alpha.cpp ---------------------
#include "alpha.h"
namespace foo {
void AlphaC::increment() {
++m_data;
} // end increment()
} // end namespace foo
const double foo::AlphaC::m_doubl = 1.0; // This works. Why?
// alphaMain.cpp -----------------
#include "alpha.h"
int main() {
foo::AlphaC my_alpha;
my_alpha.increment();
return 0;
} // end main()