473,240 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,240 software developers and data experts.

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 3511
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: bingomanatee | last post by:
I am having a problem with inheritance and static properties. I am writing a data record class object structure for Xoops. Because the table name is dynamic, I have to set it as a static member...
15
by: Alfonso Morra | last post by:
Hi, I have some code from an example, that I want to retrofit into my project. The code from the example has the following line: SharedAppenderPtr myAppender( new...
3
by: geoffblanduk_nospam | last post by:
Given a set of intervals {i1, i2, i3, ...} a list is produced; the base items (0) are placed one at a time into the stream and after the ix one a interval item (1, 2, 3, etc) of the correct type is...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
2
by: Barrie | last post by:
I'm having a quite complicate situation, and it doesn't want to compile. The situation is as follows: I have three classes, let's call them A, B and C. They each have a .h and a .cpp file. All...
3
by: lars.uffmann | last post by:
Hi everyone! I am debugging a big piece of code on the search for memory leaks, using g++ under suse 9.3. Since I'm trying to eliminate ALL memory leaks, I now stumbled upon a class foo that is...
5
by: wshaer | last post by:
Hi This is the task: and these are my classes: public class Engine{ // Declare the varibles
13
by: Francois Appert | last post by:
This post was originally in the C# Corner site, but their server is down. I'd like to see if this group can answer. I program in C++ and am learning C#. The issue is: why should anybody...
8
by: Rahul | last post by:
Hi, I have the following code and i get a compilation error, int main() { class Locale { public: static int c;
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.