Skip to main content
GameDev.net gamedev.net
🔒 Locked

STL map question

Started by foofightr May 31, 2003 at 4:34 PM 11 replies 2.4k views
Original Post
foofightr
foofightr
Sorry but I can't make sense of the error VC++ gives me.
typedef std::map<sockaddr_in, client_t *> myMapType;

myMapType myMap;

sockaddr_in sock;
client_t *client = something;
myMap[sock] = client;
   
I get this error:

c:\program files\microsoft visual studio\vc98\include\functional(86) : error C2784: 'bool __cdecl std::operator <(const class std::multimap<_K,_Ty,_Pr,_A> &,const 
class std::multimap<_K,_Ty,_Pr,_A> &)' : could not deduce template argument for 'const class std::multimap<_K,_Ty,_Pr,_A> &' from 'const struct sockaddr_in'
        c:\program files\microsoft visual studio\vc98\include\functional(86) : while compiling class-template member function 'bool __thiscall std::less::operator ()
(const struct sockaddr_in &,const struct sockaddr_in &) const'
            
Something about not being able to deduce the sockaddr_in argument. Anyone know why? (the help files are no help) [edited by - foofightr on May 31, 2003 5:37:37 PM] [edited by - foofightr on May 31, 2003 5:43:15 PM]
mfawcett
mfawcett
std::map is a template class. You must provide template arguments when declaring an instance of it. The compiler tells you that since you didn't give it a template argument, it can't deduce it for you (compilers don't read minds ).

to declare a std::map that holds integers, you could do:

std::map<int> myMap;



[edited by - mfawcett on May 31, 2003 5:41:17 PM]

edit: although posting here without source or code tags makes those less than/greater than signs go away, so maybe it's something else, but without seeing the actual code I can't say.

[edited by - mfawcett on May 31, 2003 5:42:45 PM]
--Michael Fawcett
billybob
billybob
he did, the forum ate it. quote or edit his post to see what it is
foofightr
foofightr
Yes, the part in brackets was eaten by the board. I hate using source box just for this, but here goes.

see original post that was edited

[edited by - foofightr on May 31, 2003 5:43:34 PM]
Qw3r7yU10p!
Qw3r7yU10p!
I think it means you need less-than defined for sockaddr_in


struct sockaddr_in {
bool lt(const sockaddr_in& rhs);
};

bool operator<(const sockaddr_in& lhs, const sockaddr_in& rhs) {
return lhs.lt(rhs);
}

should fix it for you
mfawcett
mfawcett
std::map's use the less than operator for storing. Does sockaddr_in have an one? If not, you need one for it to be a key.



[edited by - mfawcett on May 31, 2003 5:45:36 PM]
--Michael Fawcett
neoztar
neoztar
How is sokcaddr_in defined? If it's a class, it'll need overloaded comparison operators for use with std::map.

EDIT: Argh! beaten...

~nz 

// Website // Google // GameDev // NeHe // MSDN // OpenGL Extensions //

[edited by - neoztar on May 31, 2003 5:47:29 PM]
foofightr
foofightr
Argh. sockaddr_in is part of winsock.h, so I can''t just redefine it. Well, I could but it would only compile on my machine.

Oh well, thanks for the quick answers. Will find another solution...
Mage2k
Mage2k
or, store a pointer to it

peace and (trance) out

Mage
---------------------------------------------------There are 10 kinds of people in the world:Those that understand binary, and those that dont...Mage
Zipster
Zipster
Define a global comparison operator:

bool operator<(const sockaddr_in &l, const sockaddr_in &r)
{
return (l.someparam < r.someparam);
}


EDIT: This was already suggested. Why didn't you try it?

[edited by - Zipster on May 31, 2003 12:02:46 AM]
Beer Hunter
Beer Hunter
The map template takes three template parameters -- the third is a comparison functor which defaults to std::less, which uses the less-than operator. You can declare your own functor to compare sockaddr_ins, and use that as the third template parameter instead.
Seriema
Seriema
exactly as Beer Hunter said!

make a functor

here's a quick one

// You just gotta love the name...

struct SockaddrInCompareLess : public std::binary_function<sockaddr_in,sockaddr_in,bool>
{
public:
bool operator () (const sockaddr_in& lhs, const sockaddr_in& rhs) const
{
return lhs.something < rhs.something;
}
};

// then when you declare your map just:

typedef std::map<sockaddr_in, client_t *, SockaddrInCompareLess ()> myMapType;


should work like a charm

PS. some prefer to write the operator() as:
result_type operator () (const first_parameter_type& lhs, const second_parameter_type& rhs)
if you're unfamiliar with std::binary_function it's just a simple templated struct that has some typedefs (like those in the alternative op() declaration) which are needed sometimes.

"No lies of sugar can sweeten the sournes of reality"

}+TITANIUM+{ A.K.A. DXnewbie[onMIRC]

[edited by - Seriema on June 1, 2003 7:35:27 AM]
[ ThumbView: Adds thumbnail support for DDS, PCX, TGA and 16 other imagetypes for Windows XP Explorer. ] [ Chocolate peanuts: Brazilian recipe for home made c

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.