On Sat, 31 Jul 2004 12:44:20 +0100, John Harrison
<jo*************@hotmail.com> wrote:
On Sat, 31 Jul 2004 11:29:58 GMT, <an*******@coolgroups.com> wrote:
hello,
i am attempting to initialize a constant in my .h file. it would be
terrific if i
could also assign the constant, but i continue to get compile
problems. this
is what i'm trying to do:
(in myfile.h)
const std::string MY_CONSTANT = "permanent value";
but my compiler (gcc version 3.2.1) tells me:
ISO C++ forbids initialization of member
any feedback is appreciated
Well C++ does indeed forbid in-class initialisation of constants, except
for integral constants.
So you have to initialise your constant outside the class, in a source
file, instead. Is there any reason why this is a problem?
john
I missed that you haven't declared your constant as static, is there any
reason for this?
Normally you would do something like this
// in header file
class MyClass
{
static std::string MY_CONSTANT;
};
// in source file
std::string MyClass::MY_CONSTANT = "permanent value";
john