Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ STL list
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?
#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 23-May-11 5:34am
gones102
Edited 23-May-11 6:00am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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;
   }
};(
  Permalink  
Comments
SAKryukov at 23-May-11 11:23am
   
Nice sample and explanation, my 5.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
std::list elements must be equality comparable. Add an operator == () to your struct mem_list like:
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
  Permalink  
v2
Comments
Stefan63 at 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 at 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)



Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 23 May 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100