P
Philip Potter
I'm reading the comp.lang.c++ faq and, though it is incredibly clear and
lucid in many points, I am completely confused by question 29.6:
http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.6
"Here is another even more common example:
class Fred {
public:
...
private:
static const int max_ = 107;
...
};
In this example, you would need to add the line int Fred::max_; in exactly
one .cpp file, typically in Fred.cpp."
Firstly, shouldn't it say "const int Fred::max_"?
Secondly, it indicates the following code is correct (modulo the _tmain and
tchar nonsense):
//foo.cpp
#include "jimmy.h"
#include <tchar.h>
int _tmain(int argc, _TCHAR* argv[]) {
while(jimmy::foo==5);
return 0;
}
//jimmy.h
class jimmy {
public:
static const int foo=5;
};
//jimmy.cpp
#include "jimmy.h"
const int jimmy::foo;
This code fails to compile under MSVC++, with the following output:
Compiling...
foo.cpp
Linking...
foo.obj : error LNK2005: "public: static int const jimmy::foo"
(?foo@jimmy@@2HB) already defined in jimmy.obj
Is the compiler misbehaving? Or is the FAQ misleading? Or am I just
confused?
When I remove the line "const int jimmy::foo;" from jimmy.cpp it compiles
fine under MSVC++; and if I also replace _tmain() with main(), and TCHAR
with char and so on, it compiles fine with "g++ -W -Wall -ansi -pedantic
*.cpp -ofoo". If I am supposed to be defining this static variable in one
compilation unit, g++ isn't detecting this as non-standard C++.
I can't find any reference to this in TC++PL, 3rd Ed, to clarify my
confusion. I've tried checking the index under "static member" and no
mention is made of the need to define static members in exactly one
compilation unit.
What's going on? Do I have to define it or not?
Philip
lucid in many points, I am completely confused by question 29.6:
http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.6
"Here is another even more common example:
class Fred {
public:
...
private:
static const int max_ = 107;
...
};
In this example, you would need to add the line int Fred::max_; in exactly
one .cpp file, typically in Fred.cpp."
Firstly, shouldn't it say "const int Fred::max_"?
Secondly, it indicates the following code is correct (modulo the _tmain and
tchar nonsense):
//foo.cpp
#include "jimmy.h"
#include <tchar.h>
int _tmain(int argc, _TCHAR* argv[]) {
while(jimmy::foo==5);
return 0;
}
//jimmy.h
class jimmy {
public:
static const int foo=5;
};
//jimmy.cpp
#include "jimmy.h"
const int jimmy::foo;
This code fails to compile under MSVC++, with the following output:
Compiling...
foo.cpp
Linking...
foo.obj : error LNK2005: "public: static int const jimmy::foo"
(?foo@jimmy@@2HB) already defined in jimmy.obj
Is the compiler misbehaving? Or is the FAQ misleading? Or am I just
confused?
When I remove the line "const int jimmy::foo;" from jimmy.cpp it compiles
fine under MSVC++; and if I also replace _tmain() with main(), and TCHAR
with char and so on, it compiles fine with "g++ -W -Wall -ansi -pedantic
*.cpp -ofoo". If I am supposed to be defining this static variable in one
compilation unit, g++ isn't detecting this as non-standard C++.
I can't find any reference to this in TC++PL, 3rd Ed, to clarify my
confusion. I've tried checking the index under "static member" and no
mention is made of the need to define static members in exactly one
compilation unit.
What's going on? Do I have to define it or not?
Philip