473,769 Members | 4,089 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

getting values in a map - map wrapper class

I am writing a class as a wrapper around a std::map.

The class is:
template<class TKey, class TValue>
class CGeneralMap : protected map<TKey, TValue>

At the moment I have basic functions like Add, Remove etc but I am
really struggling with GetValue, which will be like the value obtained
from [key]

Eg if I create a:
CGeneralMap<int , std::stringTest Map;

and I have a key 3 with std::string value of John. then how in my
GetValue function do I extract the "John"?
In my GetValue function I tried:

value = [3];

but that doesn't work.

Oct 1 '07 #1
12 3579
On Oct 2, 12:28 am, Angus <anguscom...@gm ail.comwrote:
I am writing a class as a wrapper around a std::map.

The class is:
template<class TKey, class TValue>
class CGeneralMap : protected map<TKey, TValue>

At the moment I have basic functions like Add, Remove etc but I am
really struggling with GetValue, which will be like the value obtained
from [key]

Eg if I create a:
CGeneralMap<int , std::stringTest Map;

and I have a key 3 with std::string value of John. then how in my
GetValue function do I extract the "John"?

In my GetValue function I tried:

value = [3];

but that doesn't work.

std::string getKeyValue( int index ) const
{
// Code...

return TestMap[ index ];
}

--
Chris Val

Oct 1 '07 #2
On 1 Oct, 15:38, "Chris ( Val )" <chris...@gmail .comwrote:
On Oct 2, 12:28 am, Angus <anguscom...@gm ail.comwrote:


I am writing a class as a wrapper around a std::map.
The class is:
template<class TKey, class TValue>
class CGeneralMap : protected map<TKey, TValue>
At the moment I have basic functions like Add, Remove etc but I am
really struggling with GetValue, which will be like the value obtained
from [key]
Eg if I create a:
CGeneralMap<int , std::stringTest Map;
and I have a key 3 with std::string value of John. then how in my
GetValue function do I extract the "John"?
In my GetValue function I tried:
value = [3];
but that doesn't work.

std::string getKeyValue( int index ) const
{
// Code...

return TestMap[ index ];
}

--
Chris Val- Hide quoted text -

- Show quoted text -
Yes of course I understand that. But I am talking about from within
my class. From within my GetValue function.
Oct 1 '07 #3
On Oct 2, 1:04 am, Angus <anguscom...@gm ail.comwrote:
On 1 Oct, 15:38, "Chris ( Val )" <chris...@gmail .comwrote:


On Oct 2, 12:28 am, Angus <anguscom...@gm ail.comwrote:
I am writing a class as a wrapper around a std::map.
The class is:
template<class TKey, class TValue>
class CGeneralMap : protected map<TKey, TValue>
At the moment I have basic functions like Add, Remove etc but I am
really struggling with GetValue, which will be like the value obtained
from [key]
Eg if I create a:
CGeneralMap<int , std::stringTest Map;
and I have a key 3 with std::string value of John. then how in my
GetValue function do I extract the "John"?
In my GetValue function I tried:
value = [3];
but that doesn't work.
std::string getKeyValue( int index ) const
{
// Code...
return TestMap[ index ];
}
--
Chris Val- Hide quoted text -
- Show quoted text -

Yes of course I understand that. But I am talking about from within
my class. From within my GetValue function.- Hide quoted text -
What am I missing?
What I showed you can be a member function if you put it in a class.

--
Chris Val

Oct 1 '07 #4
Angus wrote:
I am writing a class as a wrapper around a std::map.

The class is:
template<class TKey, class TValue>
class CGeneralMap : protected map<TKey, TValue>

At the moment I have basic functions like Add, Remove etc but I am
really struggling with GetValue, which will be like the value obtained
from [key]

Eg if I create a:
CGeneralMap<int , std::stringTest Map;

and I have a key 3 with std::string value of John. then how in my
GetValue function do I extract the "John"?
In my GetValue function I tried:

value = [3];

but that doesn't work.
You could of course use
value = (*this)[3];

But I think the "optimal" solution will be
value = operator[](3);

Both of those call the "operator[]" of the std::map, which is what you want.

Cheers,
Daniel
--
Got two Dear-Daniel-Instant Messages
by MSN, associate ICQ with stress--so
please use good, old E-MAIL!
Oct 1 '07 #5
"Chris ( Val )" <chris...@gmail .comwrote:
std::string getKeyValue( int index ) const
{
// Code...

return TestMap[ index ];
Can std::map<operat or [](size_t) be used in a const member function?
}
Oct 1 '07 #6
On 1 Oct, 18:32, Daniel Kraft <d...@domob.euw rote:
Angus wrote:
I am writing a class as a wrapper around a std::map.
The class is:
template<class TKey, class TValue>
class CGeneralMap : protected map<TKey, TValue>
At the moment I have basic functions like Add, Remove etc but I am
really struggling with GetValue, which will be like the value obtained
from [key]
Eg if I create a:
CGeneralMap<int , std::stringTest Map;
and I have a key 3 with std::string value of John. then how in my
GetValue function do I extract the "John"?
In my GetValue function I tried:
value = [3];
but that doesn't work.

You could of course use
value = (*this)[3];

But I think the "optimal" solution will be
value = operator[](3);

Both of those call the "operator[]" of the std::map, which is what you want.

Cheers,
Daniel

--
Got two Dear-Daniel-Instant Messages
by MSN, associate ICQ with stress--so
please use good, old E-MAIL!- Hide quoted text -

- Show quoted text -
Ah brilliant, that is what I needed. But now I have a conversion
problem.

If on line 366 I have:
Value = operator[](Key);

and on line 367 I have:
Value = (*this)[Key];

Then I get these compilation errors in Microsoft Visual C++ version 6:
TestBed.cpp
generalmap.h(36 6) : error C2662: '[]' : cannot convert 'this' pointer
from
'const class CGeneralMap<int ,class std::basic_stri ng<char,struct
std::char_trait s<char>,class std::allocator< char >'

to

'class std::map<int,cl ass std::basic_stri ng<char,struct
std::char_trait s<char>,class std::allocator< char,struct
std::less<int>, class std::allocator< class
std::basic_stri ng<char,struct std::char_trait s<char>,class
std::allocator< char &'

Conversion loses qualifiers
c:\program files\microsoft visual studio\vc98\inc lude
\xmemory(59) : while compiling class-template member function 'void
__thiscall CGeneralMap<int ,class std::basic_stri ng<char,struct
std::char_trait s<char>,class std::allocator< char >::G
etValue(const int,class std::basic_stri ng<char,struct
std::char_trait s<char>,class std::allocator< char &) const'

generalmap.h(36 7) : error C2678: binary '[' : no operator defined
which takes a left-hand operand of type 'const class
CGeneralMap<int ,class std::basic_stri ng<char,struct std::char_trait s<
char>,class std::allocator< char >' (or there is no acceptable
conversion)
c:\program files\microsoft visual studio\vc98\inc lude
\xmemory(59) : while compiling class-template member function 'void
__thiscall CGeneralMap<int ,class std::basic_stri ng<char,struct
std::char_trait s<char>,class std::allocator< char >::G
etValue(const int,class std::basic_stri ng<char,struct
std::char_trait s<char>,class std::allocator< char &) const'

ie I am trying both options.

Oct 1 '07 #7
On 1 Oct, 18:32, Daniel Kraft <d...@domob.euw rote:
Angus wrote:
I am writing a class as a wrapper around a std::map.
The class is:
template<class TKey, class TValue>
class CGeneralMap : protected map<TKey, TValue>
At the moment I have basic functions like Add, Remove etc but I am
really struggling with GetValue, which will be like the value obtained
from [key]
Eg if I create a:
CGeneralMap<int , std::stringTest Map;
and I have a key 3 with std::string value of John. then how in my
GetValue function do I extract the "John"?
In my GetValue function I tried:
value = [3];
but that doesn't work.

You could of course use
value = (*this)[3];

But I think the "optimal" solution will be
value = operator[](3);

Both of those call the "operator[]" of the std::map, which is what you want.

Cheers,
Daniel

--
Got two Dear-Daniel-Instant Messages
by MSN, associate ICQ with stress--so
please use good, old E-MAIL!- Hide quoted text -

- Show quoted text -
Its ok I had to fiddle with const qualifiers. It now works fine.

Oct 1 '07 #8
On Oct 2, 1:04 am, Angus <anguscom...@gm ail.comwrote:
On 1 Oct, 15:38, "Chris ( Val )" <chris...@gmail .comwrote:


On Oct 2, 12:28 am, Angus <anguscom...@gm ail.comwrote:
I am writing a class as a wrapper around a std::map.
The class is:
template<class TKey, class TValue>
class CGeneralMap : protected map<TKey, TValue>
At the moment I have basic functions like Add, Remove etc but I am
really struggling with GetValue, which will be like the value obtained
from [key]
Eg if I create a:
CGeneralMap<int , std::stringTest Map;
and I have a key 3 with std::string value of John. then how in my
GetValue function do I extract the "John"?
In my GetValue function I tried:
value = [3];
but that doesn't work.
std::string getKeyValue( int index ) const
{
// Code...
return TestMap[ index ];
}

Yes of course I understand that. But I am talking about from within
my class. From within my GetValue function.
In future, please try to describe what you're having difficulty
with a bit more clearly. You stated that you were having difficulty
with your member function "GetValue", not operator[] which is quite
different.

While you're at it, ensure that you check for the key's existence.

--
Chris Val



Oct 1 '07 #9
On Oct 2, 1:35 am, yanlinli...@gma il.com wrote:
"Chris ( Val )" <chris...@gmail .comwrote:
std::string getKeyValue( int index ) const
{
// Code...
return TestMap[ index ];

Can std::map<operat or [](size_t) be used in a const member function?
I don't think so.

--
Chris Val

Oct 1 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
9600
by: Fernando Rodriguez | last post by:
Hi, I need to traverse the methods defined in a class and its superclasses. This is the code I'm using: # An instance of class B should be able to check all the methods defined in B #and A, while an instance of class C should be able to check all methods #defined in C, B and A. #------------------------------------------------
66
5033
by: Darren Dale | last post by:
Hello, def test(data): i = ? This is the line I have trouble with if i==1: return data else: return data a,b,c,d = test()
12
4101
by: Egil M?ller | last post by:
Is there any way to create transparent wrapper objects in Python? I thought implementing __getattribute__ on either the wrapper class or its metaclass would do the trick, but it does not work for the built in operators: class Foo(object): class __metaclass__(type): def __getattribute__(self, name): print "Klass", name
8
3496
by: Philip Lawatsch | last post by:
Hi, I'd like to get rid of all the cout stuff in my code. Following situation: Way over 2000 files, search and replace in all of them is not an option. All of the files include a basic header. In this basic header i must NOT include iostream. Iostereams thus get included after my basic header, at any random place
1
2742
by: tg.foobar | last post by:
my setup: visual studio 2005 sql server 2000 i'm using a dataset (used to be called typed dataset in 2003), where i use the MSDataSetGenerator to create a class for me based on the scheme of my SQL server. This schema doesn't include the default values that are defined in the SQL server. It's setting all the default values to NULL. is thre an easy way to get around this? for example, i have a
16
3449
by: utab | last post by:
Dear all, In programming terminology, what is a wrapper and where is it used? Regards
3
3315
by: winkatl1213 | last post by:
Hello, I am working with comtypes to interface Microsoft's DirectShow library. First, I found a Type Library on the internet that was created for accessing DirectShow from .NET. It seems that DirectShow only comes with IDL files and no type library. This got me started.
7
16292
by: pooba53 | last post by:
I am working with VB .NET 2003. Let's say my main form is called Form1. I have to launch a new form (Form2) that gathers input from the user. How can I pass variable information back to Form1 before calling the Me.close() on Form2? -Stumped
5
1455
by: Larry Bud | last post by:
I'm writing a class to create a specifically formatted fixed width file. It's 800 characters wide, consisting of approx 30 fields. So I need to pass 30 variables, maybe 10 are required. Should I write the function to accept these as parameters in a method, or should I make them properties? Can I make certain properties required?
0
9579
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9422
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10208
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9987
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9857
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8867
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3558
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2812
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.