Apr 2, 2013 at 10:15pm UTC
Hi,
I have a following problem:
I want to use set that contains map (s).
Here are important parts of my code:
1 2 3 4 5 6 7 8
struct position{
int t[2];
};
//...
//somewhere in main:
map< position, bool > p;
set < map <position, bool > > container;
container.insert(p);
and I'm getting this error:
>main.obj : error LNK2001: unresolved external symbol "bool __cdecl operator<(class std::map<struct position,bool,struct std::less<struct position>,class std::allocator<struct std::pair<struct position const ,bool> > > const &,class std::map<struct position,bool,struct std::less<struct position>,class std::allocator<struct std::pair<struct position const ,bool> > > const &)" (??M@YA_NABV?$map@Upozycja@@_NU?$less@Uposition@@@std@@V?$allocator@U?$pair@$$CBUposition@@_N@std@@@3@@std@@0@Z)
What do I wrong?
Last edited on Apr 2, 2013 at 10:52pm UTC
Apr 2, 2013 at 10:56pm UTC
std::map wants to know how to sort your positions in order, but it can't figure out how. You need to let it know how:
1 2 3 4 5 6 7 8
struct position{
int t[2];
bool operator <(const position &other) const
{
return t[0] < other.t[0] || t[1] < other.t[1];
}
};
With this change, your code compiles for me. I would still heed cire's post, though.
Last edited on Apr 2, 2013 at 11:14pm UTC
Apr 2, 2013 at 11:06pm UTC
@cire:
complexity is very important for me here, and set has quick access with its sorted items;
@L B
I have this operator< and it sorts my positions correctly (edited)
1 2 3 4 5 6 7
bool operator < ( const position &a, const position &b ){
if ( a.t[0] < b.t[0] )
return true ;
if ( a.t[0] == b.t[0] && a.t[1] < b.t[1] )
return true ;
return false ;
};
Last edited on Apr 2, 2013 at 11:14pm UTC
Apr 2, 2013 at 11:08pm UTC
@pcej so is the problem solved? Your response is not very clear.
Apr 2, 2013 at 11:11pm UTC
not really, it doesnt work and there is no progress
Apr 2, 2013 at 11:14pm UTC
Your version of the operator is equivalent to my version.
Are you sure it still does not work? When I compile your code including your version of the operator, it compiles and runs successfully.
Apr 2, 2013 at 11:21pm UTC
1 2 3 4 5 6 7
int countNext( map<position, bool > p, test t){
set < map <position, bool > > container;
container.insert( p);
return 0;
};
This is the function that I'm starting to write down. If I comment line 4 it compiles.
Do you have any other ideas?
PS.
I've tried with all stl containers and only vector compiles, but .find() is necessery for me
Last edited on Apr 3, 2013 at 1:29am UTC
Apr 3, 2013 at 1:02am UTC
Does pozycja have an overloaded operator< ?
Apr 3, 2013 at 1:38am UTC
My question remains, but for position .
Last edited on Apr 3, 2013 at 2:51am UTC
Apr 3, 2013 at 2:44am UTC
do I need any overloaded operator<? why ? I was told set is able to compare maps(or other stl containers) just like that without operator. ;/
bool operator < ( const map <pozycja, bool > &a, const map <pozycja, bool > &b );
That's the only one operator that I have. Should I write an operator< for set as well ?
edited: solved
Last edited on Apr 3, 2013 at 2:48am UTC