468,457 Members | 1,578 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,457 developers. It's quick & easy.

Problem using STL list and map objects as static member variables

I am writing a small parser object. I need to store keywords etc in
lsts. Because this data is to be shared by all instances of my parser
class, I have declared the variable as class variables (i.e. statics).

//declarations in parser class (private section)
static list<stringm_keywords, m_symbols_used;
static map<string, myParser::FuncDatam_mapped_funcs ;

I have initialization code like this:

m_symbols_used.clear();

//loadup keywords
m_keywords.clear()
m_keywords.push_back(ABC) ;
m_keywords.push_back(CDE) ;

...

I obviously can't place this in the constructor since they are statics -
(actually, I tried but I had linkage errors). Any ideas as to how to
initialize these variales ?

Jul 21 '06 #1
15 3202
Bit byte wrote:
I am writing a small parser object. I need to store keywords etc in
lsts. Because this data is to be shared by all instances of my parser
class, I have declared the variable as class variables (i.e. statics).

//declarations in parser class (private section)
static list<stringm_keywords, m_symbols_used;
static map<string, myParser::FuncDatam_mapped_funcs ;

I have initialization code like this:

m_symbols_used.clear();

//loadup keywords
m_keywords.clear()
m_keywords.push_back(ABC) ;
m_keywords.push_back(CDE) ;

...

I obviously can't place this in the constructor since they are
statics - (actually, I tried but I had linkage errors). Any ideas as
to how to initialize these variales ?
What you usually do is have a global dummy variable of type 'int' or any
other, and initialise it from a function, inside which you will put your
class-wide member manipulation.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 21 '06 #2
Bit byte wrote:
I am writing a small parser object. I need to store keywords etc in
lsts. Because this data is to be shared by all instances of my parser
class, I have declared the variable as class variables (i.e. statics).

//declarations in parser class (private section)
static list<stringm_keywords, m_symbols_used;
static map<string, myParser::FuncDatam_mapped_funcs ;

I have initialization code like this:

m_symbols_used.clear();

//loadup keywords
m_keywords.clear()
m_keywords.push_back(ABC) ;
m_keywords.push_back(CDE) ;

...

I obviously can't place this in the constructor since they are statics -
(actually, I tried but I had linkage errors). Any ideas as to how to
initialize these variales ?
A simple way to simulate an static constructor is:

class A {
class InitClass {
InitClass ();
};
static InitiClass initiclass;
friend class InitClass;
};

A:InitClass A:initiclass;

A::InitClass::InitClass ()
{
// Anyway you need no initialize statics of A
}

Some verbosity, but no additional data member required. And you can put some
static members inside InitClass to make the code cleaner.

--
Salu2
Jul 21 '06 #3


Bit byte wrote:
I am writing a small parser object. I need to store keywords etc in
lsts. Because this data is to be shared by all instances of my parser
class, I have declared the variable as class variables (i.e. statics).

//declarations in parser class (private section)
static list<stringm_keywords, m_symbols_used;
static map<string, myParser::FuncDatam_mapped_funcs ;

I have initialization code like this:

m_symbols_used.clear();

//loadup keywords
m_keywords.clear()
m_keywords.push_back(ABC) ;
m_keywords.push_back(CDE) ;

...

I obviously can't place this in the constructor since they are statics -
(actually, I tried but I had linkage errors). Any ideas as to how to
initialize these variales ?

Hmmm, judging by the feedback I've had so far, I'd probably make my
self clearer:

This is an (abridged version of) my class.

class MyParser
{
public:
MyParser();
MyParser(const string);
MyParser(const MyParser&);
MyParser& operator= (const MyParser&);

private:
typedef struct { string name, int argc } FuncData ;

static list<stringm_keywords ;
static map<string,FuncDatam_mapped_funcs ;
};

I wanted to know how can I initialize (i.e. populate) the static (and
private) member variables ?
Jul 21 '06 #4
Bit byte wrote:
Bit byte wrote:
>I am writing a small parser object. I need to store keywords etc in
lsts. Because this data is to be shared by all instances of my parser
class, I have declared the variable as class variables (i.e.
statics). //declarations in parser class (private section)
static list<stringm_keywords, m_symbols_used;
static map<string, myParser::FuncDatam_mapped_funcs ;

I have initialization code like this:

m_symbols_used.clear();

//loadup keywords
m_keywords.clear()
m_keywords.push_back(ABC) ;
m_keywords.push_back(CDE) ;

...

I obviously can't place this in the constructor since they are
statics - (actually, I tried but I had linkage errors). Any ideas as
to how to initialize these variales ?


Hmmm, judging by the feedback I've had so far, I'd probably make my
self clearer:

This is an (abridged version of) my class.

class MyParser
{
public:
MyParser();
MyParser(const string);
MyParser(const MyParser&);
MyParser& operator= (const MyParser&);

private:
typedef struct { string name, int argc } FuncData ;

static list<stringm_keywords ;
static map<string,FuncDatam_mapped_funcs ;
Add here:
static int keyword_and_funcs_init_function();
static int dummy;
};

I wanted to know how can I initialize (i.e. populate) the static (and
private) member variables ?
Where you define the 'm_keywords', right after them, add:

int MyParser::dummy = MyParser::keyword_and_funcs_init_function();

/* static */ int MyParser::keyword_and_funcs_init_function()
{
/* do your stuffing of 'm_keywords' and 'm_mapped_funcs' here */
return 42;
}

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 21 '06 #5
Bit byte wrote:
>

Bit byte wrote:
>I am writing a small parser object. I need to store keywords etc in
lsts. Because this data is to be shared by all instances of my parser
class, I have declared the variable as class variables (i.e. statics).

//declarations in parser class (private section)
static list<stringm_keywords, m_symbols_used;
static map<string, myParser::FuncDatam_mapped_funcs ;

I have initialization code like this:

m_symbols_used.clear();

//loadup keywords
m_keywords.clear()
m_keywords.push_back(ABC) ; m_keywords.push_back(CDE) ;

...

I obviously can't place this in the constructor since they are statics
- (actually, I tried but I had linkage errors). Any ideas as to how to
initialize these variales ?


Hmmm, judging by the feedback I've had so far, I'd probably make my
self clearer:

This is an (abridged version of) my class.

class MyParser
{
public:
MyParser();
MyParser(const string);
MyParser(const MyParser&);
MyParser& operator= (const MyParser&);

private:
typedef struct { string name, int argc } FuncData ;

static list<stringm_keywords ;
static map<string,FuncDatam_mapped_funcs ;
};

I wanted to know how can I initialize (i.e. populate) the static (and
private) member variables ?

What was wrong with Victor's suggestion? Here is an example
illustrating his idea which seems to be essentially what you need:

---begin code

#include <list>
#include <iostream>
#include <iterator>
#include <algorithm>

using namespace std;

struct A
{
static list<int li;
static int initializeLi ();
};

int A::initializeLi ()
{
li.push_back(0);
li.push_back(1);
li.push_back(2);
return 0;
}

list<intA::li;
const int throwAway = A::initializeLi();

int main ()
{
copy(A::li.begin( ), A::li.end(), ostream_iterator<int>(cout,"\n"));
}

---end code
Jul 21 '06 #6
Mark P posted:
struct A
{
static list<int li;
static int initializeLi ();
};

int A::initializeLi ()
{
li.push_back(0);
li.push_back(1);
li.push_back(2);
return 0;
}

list<intA::li;
const int throwAway = A::initializeLi();

I'd probably use a "static constructor":

#include <list>

struct A {

std::list<intstatic li;

private:

struct StaticConstructor {

StaticConstructor()
{
A::li.push_back(0);
A::li.push_back(1);
A::li.push_back(2);
}

} const static stctr;

};
std::list<intA::li;

int main() {}

--

Frederick Gotham
Jul 21 '06 #7


Victor Bazarov wrote:
Bit byte wrote:
>>Bit byte wrote:

>>>I am writing a small parser object. I need to store keywords etc in
lsts. Because this data is to be shared by all instances of my parser
class, I have declared the variable as class variables (i.e.
statics). //declarations in parser class (private section)
static list<stringm_keywords, m_symbols_used;
static map<string, myParser::FuncDatam_mapped_funcs ;

I have initialization code like this:

m_symbols_used.clear();

//loadup keywords
m_keywords.clear()
m_keywords.push_back(ABC) ;
m_keywords.push_back(CDE) ;

...

I obviously can't place this in the constructor since they are
statics - (actually, I tried but I had linkage errors). Any ideas as
to how to initialize these variales ?


Hmmm, judging by the feedback I've had so far, I'd probably make my
self clearer:

This is an (abridged version of) my class.

class MyParser
{
public:
MyParser();
MyParser(const string);
MyParser(const MyParser&);
MyParser& operator= (const MyParser&);

private:
typedef struct { string name, int argc } FuncData ;

static list<stringm_keywords ;
static map<string,FuncDatam_mapped_funcs ;


Add here:
static int keyword_and_funcs_init_function();
static int dummy;

>>};

I wanted to know how can I initialize (i.e. populate) the static (and
private) member variables ?


Where you define the 'm_keywords', right after them, add:

int MyParser::dummy = MyParser::keyword_and_funcs_init_function();

/* static */ int MyParser::keyword_and_funcs_init_function()
{
/* do your stuffing of 'm_keywords' and 'm_mapped_funcs' here */
return 42;
}

V
Thanks Victor, I understand now. However, when I apply the modifications
you sugested, I'm still getting linkage errors:

error LNK2001: unresolved external symbol "private: static class
std::list<class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char,class
std::allocator<class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char
MyParser::m_keywords"
(?m_keywords@MyParser@@0V?$list@V?$basic_string@DU ?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$all ocator@V?$basic_string@DU?$char_traits@D@std@@V?$a llocator@D@2@@std@@@2@@std@@A)

I can't imagine why I'm getting this linkage error - any ideas ? (I've
run out of ideas)

Jul 21 '06 #8
Bit byte wrote:
Victor Bazarov wrote:
[..]
>Where you define the 'm_keywords', right after them, [..]

Thanks Victor, I understand now. However, when I apply the
modifications you sugested, I'm still getting linkage errors:

error LNK2001: unresolved external symbol "private: static class
std::list<class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char,class
std::allocator<class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char
MyParser::m_keywords"
(?m_keywords@MyParser@@0V?$list@V?$basic_string@DU ?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$all ocator@V?$basic_string@DU?$char_traits@D@std@@V?$a llocator@D@2@@std@@@2@@std@@A)

I can't imagine why I'm getting this linkage error - any ideas ? (I've
run out of ideas)
Read above. WHERE you *define* the 'm_keywords'... So where is it?
Did you forget to *define* 'm_keywords'?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 21 '06 #9


Victor Bazarov wrote:
Bit byte wrote:
>>Bit byte wrote:

>>>I am writing a small parser object. I need to store keywords etc in
lsts. Because this data is to be shared by all instances of my parser
class, I have declared the variable as class variables (i.e.
statics). //declarations in parser class (private section)
static list<stringm_keywords, m_symbols_used;
static map<string, myParser::FuncDatam_mapped_funcs ;

I have initialization code like this:

m_symbols_used.clear();

//loadup keywords
m_keywords.clear()
m_keywords.push_back(ABC) ;
m_keywords.push_back(CDE) ;

...

I obviously can't place this in the constructor since they are
statics - (actually, I tried but I had linkage errors). Any ideas as
to how to initialize these variales ?


Hmmm, judging by the feedback I've had so far, I'd probably make my
self clearer:

This is an (abridged version of) my class.

class MyParser
{
public:
MyParser();
MyParser(const string);
MyParser(const MyParser&);
MyParser& operator= (const MyParser&);

private:
typedef struct { string name, int argc } FuncData ;

static list<stringm_keywords ;
static map<string,FuncDatam_mapped_funcs ;


Add here:
static int keyword_and_funcs_init_function();
static int dummy;

>>};

I wanted to know how can I initialize (i.e. populate) the static (and
private) member variables ?


Where you define the 'm_keywords', right after them, add:

int MyParser::dummy = MyParser::keyword_and_funcs_init_function();

/* static */ int MyParser::keyword_and_funcs_init_function()
{
/* do your stuffing of 'm_keywords' and 'm_mapped_funcs' here */
return 42;
}

V
Thanks Victor, I understand now. However, when I apply the modifications
you sugested, I'm still getting linkage errors:

error LNK2001: unresolved external symbol "private: static class
std::list<class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char,class
std::allocator<class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char
MyParser::m_keywords"
(?m_keywords@MyParser@@0V?$list@V?$basic_string@DU ?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$all ocator@V?$basic_string@DU?$char_traits@D@std@@V?$a llocator@D@2@@std@@@2@@std@@A)

I can't imagine why I'm getting this linkage error - any ideas ? (I've
run out of ideas)

Jul 21 '06 #10
Bit byte wrote:
[..]
I can't imagine why I'm getting this linkage error - any ideas ? (I've
run out of ideas)
Ease up on the "Send" button, please.
Jul 21 '06 #11


Victor Bazarov wrote:
Bit byte wrote:
>>Victor Bazarov wrote:
[..]
>>>Where you define the 'm_keywords', right after them, [..]

Thanks Victor, I understand now. However, when I apply the
modifications you sugested, I'm still getting linkage errors:

error LNK2001: unresolved external symbol "private: static class
std::list<class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char,class
std::allocator<class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char
MyParser::m_keywords"
(?m_keywords@MyParser@@0V?$list@V?$basic_string@ DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$a llocator@V?$basic_string@DU?$char_traits@D@std@@V? $allocator@D@2@@std@@@2@@std@@A)

I can't imagine why I'm getting this linkage error - any ideas ? (I've
run out of ideas)


Read above. WHERE you *define* the 'm_keywords'... So where is it?
Did you forget to *define* 'm_keywords'?

V
Yes, my fault. Thanks for the clarification. Builds fine now.

Jul 21 '06 #12
Bit byte wrote:
I am writing a small parser object. I need to store keywords etc in
lsts. Because this data is to be shared by all instances of my parser
class, I have declared the variable as class variables (i.e. statics).

//declarations in parser class (private section)
static list<stringm_keywords, m_symbols_used;
static map<string, myParser::FuncDatam_mapped_funcs ;

I have initialization code like this:

m_symbols_used.clear();

//loadup keywords
m_keywords.clear()
m_keywords.push_back(ABC) ;
m_keywords.push_back(CDE) ;

...

I obviously can't place this in the constructor since they are statics -
(actually, I tried but I had linkage errors). Any ideas as to how to
initialize these variales ?
Use an initializer helper class:

class A
{
public:
// ...
private:
static const std::map<int,const char*map_;
};

Then in your .cpp file:

namespace // anonymous
{
template<class Key, class Value>
class MapInitializer
{
std::map<Key,Valuem_;
public:
operator std::map<Key,Value>() const { return m_; }

MapInitializer& Add( const Key& k, const Value& v )
{
m_[k] = v;
return *this;
}
};
}

const std::map<int,const char*A::map_
= MapInitializer<int,const char*>()
.Add( 10, "Msg 1" )
.Add( 20, "Msg 2" )
.Add( 30, "Msg 3" );

Cheers! --M

Jul 21 '06 #13
Bit byte wrote:
I am writing a small parser object. I need to store keywords etc in
lsts. Because this data is to be shared by all instances of my parser
class, I have declared the variable as class variables (i.e. statics).

//declarations in parser class (private section)
static list<stringm_keywords, m_symbols_used;
static map<string, myParser::FuncDatam_mapped_funcs ;

I have initialization code like this:

m_symbols_used.clear();

//loadup keywords
m_keywords.clear()
m_keywords.push_back(ABC) ;
m_keywords.push_back(CDE) ;

...

I obviously can't place this in the constructor since they are statics -
(actually, I tried but I had linkage errors). Any ideas as to how to
initialize these variales ?
#include <list>
#include <string>
#include <map>

class Parser
{
private:
static const class KeyWords : public std::list<std::string>
{
public:
KeyWords()
{
push_back( "ABC" );
push_back( "CDE" );
}
} m_keywords; // initialiszed with keywords

struct FuncData
{
std::string name;
int argc;
};

static std::map<std::string, FuncDatam_mapped_funcs ; // initilized
emtpty
static std::list<std::stringm_symbols_used;
};

int main()
{
Parser aParser;
}
Jul 24 '06 #14
Lars Tetzlaff wrote:
[...]
#include <list>
#include <string>
#include <map>

class Parser
{
private:
static const class KeyWords : public std::list<std::string>
{
public:
KeyWords()
{
push_back( "ABC" );
push_back( "CDE" );
}
} m_keywords; // initialiszed with keywords
This static data member doesn't seem to have been defined anywhere...
>
struct FuncData
{
std::string name;
int argc;
};

static std::map<std::string, FuncDatam_mapped_funcs ; //
initilized emtpty
static std::list<std::stringm_symbols_used;
Neither are those two static data members...
};

int main()
{
Parser aParser;
}
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 24 '06 #15
Victor Bazarov wrote:
Lars Tetzlaff wrote:
>[...]
#include <list>
#include <string>
#include <map>

class Parser
{
private:
static const class KeyWords : public std::list<std::string>
{
public:
KeyWords()
{
push_back( "ABC" );
push_back( "CDE" );
}
} m_keywords; // initialiszed with keywords

This static data member doesn't seem to have been defined anywhere...
It's not used so it's not missing ;)
> struct FuncData
{
std::string name;
int argc;
};

static std::map<std::string, FuncDatam_mapped_funcs ; //
initilized emtpty
static std::list<std::stringm_symbols_used;

Neither are those two static data members...
>};

int main()
{
Parser aParser;
}

V
add

const Parser::KeyWords Parser::m_keywords;
std::map<std::string, Parser::FuncData Parser::m_mapped_funcs ;
std::list<std::stringParser::m_symbols_used;

wherever (Parser.cpp?) you want if you need them

Lars
Jul 24 '06 #16

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by bingomanatee | last post: by
3 posts views Thread by geoffblanduk_nospam | last post: by
2 posts views Thread by ajikoe | last post: by
2 posts views Thread by Barrie | last post: by
8 posts views Thread by Rahul | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by subhajit12345 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.