In code there are two map.One store pair and other store where Values is class with 5 variable with data type string,int,string,int,int.but during inserting in the second map i am getting error g++ error: no match for ‘operator<’ in ‘__x < __y’ when trying to insert in map. (Note Keys and Values in first map changes to Values,Key in second map)
How to solve it.
class Values
{
private:
std::string C_addr;
int C_port;
std::string S_addr;
int S_port;
int C_ID;
public:
Values(std::string,int,std::string,int,int);
void printValues();
};
Values :: Values(std::string Caddr,int Cport,std::string Saddr,int Sport,int Cid)
{
C_addr=Caddr;
C_port=Cport;
S_addr=Swaddr;
S_port=Sport;
C_ID=Cid;
}
void Values::printValues()
{
cout << C_addr<<":" <<C_port<<":" << S_addr <<":" <<S_port << ":"<<C_ID <<endl;
}
map<int, Values> items;
map<Values,int> itemscopy;
Values connection (inet_ntoa(Caddr.sin_addr),ntohs(Caddr.sin_port),inet_ntoa(Saddr.sin_addr),ntohs(Saddr.sin_port),CID);
for(unsigned int key=0;key<=30000; )
{
map<int,Values>::const_iterator itemsIterator=items.find(key);
if(itemsIterator==items.end())
{
items.insert(pair<int, Values> (key, connection));
{
map<Values,int>::const_iterator itemsIterator1;
if(itemsIterator1==itemscopy.end())
itemscopy.insert(pair<Values,int> (connection, key));
}
break;
}
else
{
cout<<"already exist";
key=key+1;
}
}
The compiler does not know in which order to insert keys in the map. You have to define some order relation for class Values.
You need to define operator < for your class. For example you can do it the following way or something else
class Values
{
private:
std::string C_addr;
int C_port;
std::string S_addr;
int S_port;
int C_ID;
public:
Values(std::string,int,std::string,int,int);
void printValues();
bool operator <( const Values &rhs ) const
{
return ( C_ID < rhs.C_ID );
}
};
For your second map the key type is not compareable.
map<Values,int>
is essentially thismap<Values, int, std::less<Values>, std::allocator<std::pair<const Values, int>
.
Sinec you don't have an bool operator<
for your Value type less will not compile.
So you can either define an bool operator<
for your class or you create the map with an own comparison function.
Implement bool operator<(const Values& other) const
member function in the Values
class that will enable map<Values, int>
to sort the keys of type Values
. The map stores key-value pairs and the keys are sorted, so you need to provide the comparison operator for them. When you instantiate map<Values, int>
you are saying that you will use Values
as keys and ints
as values for that map.
Here is a small working example, where the C_ID is taken as the comparison argument for Values
:
#include <map>
class Values
{
private:
std::string C_addr;
int C_port;
std::string S_addr;
int S_port;
int C_ID;
public:
Values(std::string first,int second,std::string third,int fourth,int fifth)
:
C_addr(first),
C_port(second),
S_addr(third),
S_port(fourth),
C_ID(fifth)
{};
bool operator<(const Values& other) const
{
return C_ID < other.C_ID;
}
};
using namespace std;
int main(int argc, const char *argv[])
{
map<Values, int> mymap;
mymap.insert(std::make_pair(Values("test", 0, "me", 1, 2), 0));
return 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With