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, intmt;
};
}
int main()
{
map<string, intmsu::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. 2 2583
<ga******@gmail.comwrote in message
news:11*********************@k70g2000cwa.googlegro ups.com...
>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, intmt;
Ok, this declares the map. Now you need to define it, outside the class.
};
}
Define it here:
map<string,intmsu::Test::mt;
(I think that's right. Does it need to be initialized smoehow? Anyone
know?)
int main()
{
map<string, intmsu::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
Howard wrote:
<ga******@gmail.comwrote:
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, intmt;
Ok, this declares the map. Now you need to define it, outside the class.
Right. See the FAQ: http://www.parashift.com/c++-faq-lit...html#faq-10.11
};
}
Define it here:
map<string,intmsu::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,VMap;
Map m_;
public:
operator Map() const { return m_; }
MapInitializer& Add( const K& k, const V& v )
{
m_[k] = v;
return *this;
}
};
map<string,intmsu::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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: DanielBradley |
last post by:
Hello all,
I have recently been porting code from Linux to cygwin and came across
a problem with static const class members (discussed below). I am
seeking to determine whether I am programming...
|
by: Scott J. McCaughrin |
last post by:
The following program compiles fine but elicits this message from the
linker: "undefined reference to VarArray::funct" and thus fails.
It seems to behave as if the static data-member:...
|
by: Samee Zahur |
last post by:
Question: How do friend functions and static member functions differ in
terms of functionality? I mean, neither necessarily needs an object of
the class to be created before they are called and...
|
by: lovecreatesbeauty |
last post by:
Hello Experts,
Why static data members can be declared as the type of class which it
belongs to?
Inside a class, non-static data members such as pointers and references
can be declared as...
|
by: Adam H. Peterson |
last post by:
I just made an observation and I wondered if it's generally known (or
if I'm missing something). My observation is that static protected
members are essentially useless, only a hint to the user. ...
|
by: Mauzi |
last post by:
hi,
this may sound odd and noob like, but what is the 'big' difference
between static and non-static funcitons ?
is there any performace differnce?
what is the best way to use them ?
thnx
...
|
by: Matt |
last post by:
All of a sudden all my C# apps require the keyword static on all global fields and methods that I create. Even in the simplest of console apps. Can someone tell me what I have inadvertenly set in...
|
by: dee |
last post by:
OleDbCommand class like many .NET classes has the following description in
its help file:
"Thread Safety
Any public static (Shared in Visual Basic) members of this type are safe for...
|
by: Kevin Prichard |
last post by:
Hi all,
I've recently been following the object-oriented techiques discussed
here and have been testing them for use in a web application. There is
problem that I'd like to discuss with you...
|
by: crjjrc |
last post by:
Hi, I've got a base class and some derived classes that look something
like this:
class Base {
public:
int getType() { return type; }
private:
static const int type = 0;
};
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |