473,396 Members | 1,853 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,396 software developers and data experts.

problem with map construction

kap
Hi all,
I am facing a weired problem when I try using maps. Here is the
sample code.
Can anyone please tell me where am I going wrong.

int main() {
less<int> ltvar;
std::map<int, int> m2(less<int>());
m2[1]; // compilation fails at this point
return 0;
}

whereas this particular code works fine
int main() {
less<int> ltvar;
std::map<int, int> m2( ltvar ); // less<int>() changed to ltvar
m2[1]; // compilation succeeds
return 0;
}

I would appreciate if someone can comment on what the issue is.

Feb 15 '06 #1
4 1501
kap wrote:
Hi all,
I am facing a weired problem when I try using maps. Here is the
sample code.
Can anyone please tell me where am I going wrong.

int main() {
less<int> ltvar;
std::map<int, int> m2(less<int>());
m2[1]; // compilation fails at this point
return 0;
}
I'm not an expert but it seems, from looking at VC++EE compiler messages
(btw, always post compiler messages if you can't compile), that this is
interpreted as a _declaration of a function_ which takes a ptr to
function as an argument and returns a map.

You can get around this like so:

std::map<int, int> m2 = std::map<int, int> (less<int>());

However, there's no reason why you need to do any of this since
less<Key> is the default comparison function used by map anyway.

-Mark

whereas this particular code works fine
int main() {
less<int> ltvar;
std::map<int, int> m2( ltvar ); // less<int>() changed to ltvar
m2[1]; // compilation succeeds
return 0;
}

I would appreciate if someone can comment on what the issue is.

Feb 15 '06 #2
Mark P wrote:
kap wrote:
Hi all,
I am facing a weired problem when I try using maps. Here is the
sample code.
Can anyone please tell me where am I going wrong.

int main() {
less<int> ltvar;
std::map<int, int> m2(less<int>());
m2[1]; // compilation fails at this point
return 0;
}


I'm not an expert but it seems, from looking at VC++EE compiler messages
(btw, always post compiler messages if you can't compile), that this is
interpreted as a _declaration of a function_ which takes a ptr to
function as an argument and returns a map.

You can get around this like so:

std::map<int, int> m2 = std::map<int, int> (less<int>());

However, there's no reason why you need to do any of this since
less<Key> is the default comparison function used by map anyway.

-Mark


I knew I'd seen this in the FAQ before...

http://www.parashift.com/c++-faq-lit...html#faq-10.19
Feb 15 '06 #3
kap wrote:
Hi all,
I am facing a weired problem when I try using maps. Here is the
sample code.
Can anyone please tell me where am I going wrong.

int main() {
less<int> ltvar;
std::map<int, int> m2(less<int>());
m2[1]; // compilation fails at this point
return 0;
}

whereas this particular code works fine
int main() {
less<int> ltvar;
std::map<int, int> m2( ltvar ); // less<int>() changed to ltvar
m2[1]; // compilation succeeds
return 0;
}

I would appreciate if someone can comment on what the issue is.


It's a FAQ:

http://www.parashift.com/c++-faq-lit...html#faq-10.19
Best

Kai-Uwe Bux
Feb 15 '06 #4
kap
hey Mark thanks a lot for the prompt reply and for pointing me to the
faq.
I highly appreciate that.

Feb 15 '06 #5

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

Similar topics

1
by: Spur | last post by:
Hi all, I implemented a memory allocation/deallocation class that logs all new/delete calls (overloaded) and remembers for each allocated block where it was allocated from (using a macro that...
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...
4
by: Bangalore | last post by:
Hi all, I am finding quite difficulty in understanding the behaviour of the following program. Base class is singleton, so it should allow the creation of only one object. Eventhough it is...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
18
by: jslowery | last post by:
I am not completely knowledgable about the status of lexical scoping in Python, but it was my understanding that this was added in a long time ago around python2.1-python2.2 I am using python2.4...
19
by: Chocawok | last post by:
Some of the classes in my app are graphical. To encapsulate the graphical side of things I had created a class called "sprite" which holds a bit map and knows how to draw itself etc. The...
8
by: silversurfer2025 | last post by:
Hello everybody, I had this problem several times now and did not yet get the reasoning behind it. I have a class with a pointer as member variable, lets say a float array. Furthermore, I have two...
6
by: kwangbok.kim | last post by:
Hi. I'm major in computer science. While studying with the what kind of document, the problem got. Please help me. The lower part is the program... Programming with visual c++ 6.0 at windows xp...
7
by: pallav | last post by:
I'm having some trouble with my copy constructor. I've tried using gdb to find the bug, but it seg faults in the destructor. I'm not able to see what I'm doing wrong. Since I'm using pointers, I...
1
by: andi111 | last post by:
Good Morning All, I am having problems turning my Matter_Creation_Form.DOC into a Matter_Creation_Form.DOT. Now, Within Word, I have created a document that contains 2 Combo Boxes which,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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,...

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.