Click here to Skip to main content
15,880,651 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
I am trying to remove an element from C++ list created for user defined data type. It is throwing error since == operator on a user defined type is unknown to template. How do I overcome this problem?
C++
#include <iostream>
#include <list>

using namespace std;

 struct mem_list
    {
        unsigned int addr;
        int size;
    };


int main()
{

    list<mem_list> Mem_numbers;
    mem_list mem;
    mem_list *ptr_mem;
    mem.addr = 0xFF;
    mem.size = 10;

    Mem_numbers.push_front(mem);

    mem.addr = 0xAA;
    mem.size = 20;

    Mem_numbers.push_front(mem);


    mem.addr = 0xBB;
    mem.size = 30;

    Mem_numbers.push_front(mem);


    mem.addr = 0xCC;
    mem.size = 40;

    Mem_numbers.push_front(mem);


    mem.addr = 0xDD;
    mem.size = 50;

    Mem_numbers.push_front(mem);

    int counter = 0;

    for(list<mem_list>::const_iterator it = Mem_numbers.begin(); it != Mem_numbers.end(); ++it)
    {
        mem = *it;
        cout << mem.size<< " ";
    }

    cout << endl;

    for(list<mem_list>::const_iterator it = Mem_numbers.begin(); it != Mem_numbers.end(); ++it)
    {

            mem = *it;

            if( mem.size == 30)
            {
                Mem_numbers.remove(mem);
            }
    }


    for(list<mem_list>::const_iterator it = Mem_numbers.begin(); it != Mem_numbers.end(); ++it)
    {

            mem = *it;
            cout << mem.size<< " ";

    }

    cout << endl;

    return 0;
}
Posted
Updated 23-May-11 5:00am
v2

The function
std::list<t>::remove(const T&)</t>
searches the list for an element that is equal to the value being passed. To test equality, it invokes the operator
bool operator==(const T& v1, const T& v2)
or, alternately, the operator
bool T::operator==(const T& v) const

For built-in types such as int, the first type of operator is implicitely defined. For user-defined types however it is not. Therefore you must define one of the two forms of this operator yourself:
bool operator==(const mem_list& v1, const mem_list& v2) {
   return v1.addr==v2.addr && v1.size==v2.size;
}
// or:
struct mem_list {
   unsigned int addr;
   int size;
   bool operator==(const struct mem_size& v) const {
      return v.size==size && v.addr==addr;
   }
};(
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 23-May-11 11:23am    
Nice sample and explanation, my 5.
--SA
Hi,
std::list elements must be equality comparable. Add an operator == () to your struct mem_list like:
C++
struct mem_list
{
    bool operator == (const mem_list& other)
    {
        return (addr == other.addr) && (size == other.size);
    }
    unsigned int addr;
    int size;
};

cheers,
AR
Edited for (hopefully) correct answer
 
Share this answer
 
v2
Comments
Stefan_Lang 23-May-11 11:13am    
Actually I believe the compiler automatically adds both copy constructor and assignment operator if you don't do so yourself. (this may be subject to compiler settings though). If it weren't so, you'd get compiler errors for the above code.
Alain Rist 23-May-11 11:33am    
You are totally right, the problem is on comparison, see my edited answer. cheers,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900