Hi,
This goes along with my last question. I've been reading through the C
++ FAQ-Lite but am getting there what I got here, and that's what I
already knew, about initializing static members. They need to be
defined once, and only once, in the implementation.
Now, as mentioned earlier, I'm making a map in this application. This
particular file doesn't contain main() and so is simply an object file
that gets linked in later. Here's what I have (sorry but I can't post
the actual code):
file.h
#ifndef _FILE_H_
#define _FILE_H_
#include <map>
#define CODE1 0x01
// .... to CODEN
#define CODE1_STR "a string for hex code 1"
// .... likewise
class myClass {
static std::map <unsigned short, const char*> mCodeStrings;
// other things including a c-tor
};
#endif
file.cpp
#include "file.h"
// now if I define these static members as such
std::map mCodeStrings[CODE1] = CODE1_STR;
I get this type of error:
error C2040: 'mCodeStrings' : 'std::map [1008]' differs in levels of
indirection from 'std::map<_Kty,_Ty,_Pr>'
|| 1> with
|| 1> [
|| 1> _Kty=unsigned short,
|| 1> _Ty=const char *,
|| 1> _Pr=std::less<unsigned short>
|| 1> ]
error C2075: 'myClass::mCodeStrings' : array initialization needs
curly braces
error C2514: 'std::map' : class has no constructors
1> c:\program files\microsoft visual studio 9.0\vc\include\map|
77| see declaration of 'std::map'
So, instead, if I define them within a function body, for example in
the C-tor as follows:
myClass::myClass() {
mCodeStrings[CODE1] = CODE1_STR;
}
I get these errors:
error LNK2001: unresolved external symbol "private: static class
std::map<unsigned short,char const *,struct std::less<unsigned
short>,class std::allocator<struct std::pair<unsigned short
const ,char const *> > > myClass::mCodeStrings" (?
mUSBClassCodeStr@[EMAIL PROTECTED]
)
So, what's the magic key to defining these things in the
implementation file? I'm used to doing this with primitive data types
such as int (in fact, the class in my program has one such static
member to track the number of instances of the object).
Andy


|