Error: no match for 'operator <in' __x <__ y when trying to insert on two cards

There are two cards in the code. One pair of stores and another store, where the values ​​are classes with 5 variables with a data type of type, int, string, int, int.but, during insertion into the second map. I get a g ++ error: there is no match for 'operator <in' __x <__y when I try to insert on the card. (Note. The keys and values ​​are the first time the map is changed to values, the key is in the 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;
    }
}
+4
source share
3 answers

, . .

< . , -

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 );
    }
};
+4

. map<Values,int> map<Values, int, std::less<Values>, std::allocator<std::pair<const Values, int>. Sinec bool operator< .

, bool operator< .

+3

Introduce bool operator<(const Values& other) consta member function in the class Valuesthat allows you to map<Values, int>sort the type keys Values. Key-value pairs are stored on the map, and the keys are sorted, so you need to provide them with a comparison operator. When you create an instance map<Values, int>, you say that you will use Valuesand as keys as the values ​​for this map ints.

Here is a small working example where C_ID is taken as a 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;
}
0
source

Source: https://habr.com/ru/post/1530581/


All Articles