Static members

G

gabitoju

I want to declare a std::map as an static member of a class, so I wrote
this code:

#include <map>
#include <string>

using namespace std;

namespace msu
{
class Test
{
public:
static map<string, int> mt;
};
}

int main()
{
map<string, int> msu::Test::mt["Jan"] = 1;

return 0;
}

I've done several searches in google and all the pages show code like
the one above to declare, define and initialize static data members.
Wll, that doesn' work and I don't kno why.
When I compiled it with g++ I get this error message:

test.cpp: In function `int main()':
test.cpp:17: error: size of array `mt' has non-integral type `const
char[4]'
test.cpp:17: error: conflicting declaration 'std::map<std::string, int,
std::less<std::string>, std::allocator<std::pair<const std::string,
int> > > msu::Test::mt[1]'
test.cpp:11: error: 'msu::Test::mt' has a previous declaration as
`std::map<std::string, int, std::less<std::string>,
std::allocator<std::pair<const std::string, int> > > msu::Test::mt'
test.cpp:17: error: conversion from `int' to non-scalar type
`std::map<std::string, int, std::less<std::string>,
std::allocator<std::pair<const std::string, int> > >' requested

I appreciate your help.
 
H

Howard

I want to declare a std::map as an static member of a class, so I wrote
this code:

#include <map>
#include <string>

using namespace std;

namespace msu
{
class Test
{
public:
static map<string, int> mt;

Ok, this declares the map. Now you need to define it, outside the class.

Define it here:

map<string,int> msu::Test::mt;

(I think that's right. Does it need to be initialized smoehow? Anyone
know?)
int main()
{
map<string, int> msu::Test::mt["Jan"] = 1;

This attempts to both define the map and set one member of it. But the
compiler sees it as re-declaring mt as an array of maps with a size given by
"Jan", which isn't a valid integral constant.

Define the map separately (as shown above), and then just use it in main().
return 0;
}

I've done several searches in google and all the pages show code like
the one above to declare, define and initialize static data members.

I don't see an example like that. (I haven't looked very long, though.)
Look again, for "static member map".
Wll, that doesn' work and I don't kno why.
When I compiled it with g++ I get this error message:

test.cpp: In function `int main()':
test.cpp:17: error: size of array `mt' has non-integral type `const
char[4]'

That's because you're declaring an array, trying to pass "Jan" as the size.
test.cpp:17: error: conflicting declaration 'std::map<std::string, int,
std::less<std::string>, std::allocator<std::pair<const std::string,
int> > > msu::Test::mt[1]'
test.cpp:11: error: 'msu::Test::mt' has a previous declaration as
`std::map<std::string, int, std::less<std::string>,
std::allocator<std::pair<const std::string, int> > > msu::Test::mt'

Those are because the new declaration doesn't match the original.
test.cpp:17: error: conversion from `int' to non-scalar type
`std::map<std::string, int, std::less<std::string>,
std::allocator<std::pair<const std::string, int> > >' requested

I appreciate your help.

-Howard
 
M

mlimber

Howard said:
Ok, this declares the map. Now you need to define it, outside the class.

Right. See the FAQ:
http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.11
Define it here:

map<string,int> msu::Test::mt;

(I think that's right. Does it need to be initialized smoehow? Anyone
know?)

It will be default initialized to be empty, but one could use a helper
class to initialize it here:

template<class K, class V>
class MapInitializer
{
typedef std::map<K,V> Map;
Map m_;
public:
operator Map() const { return m_; }

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

map<string,int> msu::Test::mt
= MapInitializer<string, int>()
.Add( "Str 1", 9 )
.Add( "Str 2", 42 )
.Add( "Str 3", 314 );

This is particularly useful if that map should be a *const* static
member.

Cheers! --M
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,690
Latest member
MacGyver

Latest Threads

Top