T
t
Lippman's C++ Primer says that if you initialize a const static data
member in a class, you still need to define the member outside the
class but without an initializer.
I tried it using Visual C++ 2005 Express edition and get this error:
1>Account.obj : error LNK2005: "private: static int const
Account:eriod" (?period@Account@@0HB) already defined in Account
Main.obj
Is this one of those things that vary by compiler? Is what Lippman
said from Standard C++?
My code is basically like this (lots of stuff omitted):
// Account.h
#ifndef ACCOUNT_H
#define ACCOUNT_H
class Account
{
static const int period = 30;
};
#endif
// Account.cpp
#include "Account.h"
const int Account:eriod;
// Account Main.cpp
#include "Account.h"
int main()
{
Account acc;
}
member in a class, you still need to define the member outside the
class but without an initializer.
I tried it using Visual C++ 2005 Express edition and get this error:
1>Account.obj : error LNK2005: "private: static int const
Account:eriod" (?period@Account@@0HB) already defined in Account
Main.obj
Is this one of those things that vary by compiler? Is what Lippman
said from Standard C++?
My code is basically like this (lots of stuff omitted):
// Account.h
#ifndef ACCOUNT_H
#define ACCOUNT_H
class Account
{
static const int period = 30;
};
#endif
// Account.cpp
#include "Account.h"
const int Account:eriod;
// Account Main.cpp
#include "Account.h"
int main()
{
Account acc;
}