Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C - C++ Learning > [C++] Initializ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 6 Topic 4227 of 4370
Post > Topic >>

[C++] Initializing static member cl*****

by Andrew Falanga <af300wsm@[EMAIL PROTECTED] > Jul 30, 2008 at 10:45 AM

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
 




 6 Posts in Topic:
[C++] Initializing static member classes
Andrew Falanga <af300w  2008-07-30 10:45:38 
Re: Initializing static member classes (fixed)
Andrew Falanga <af300w  2008-07-30 11:11:40 
Re: Initializing static member classes (fixed)
Stuart Golodetz <sgolo  2008-07-31 10:47:33 
Re: Initializing static member classes (fixed)
Bart van Ingen Schenau &l  2008-07-31 20:04:04 
Re: Initializing static member classes (fixed)
a <b@[EMAIL PROTECTED]  2008-08-01 05:28:18 
Re: Initializing static member classes (fixed)
Andrew Falanga <af300w  2008-08-08 08:53:20 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Nov 21 8:13:17 CST 2008.