STL list and user defined types

Started by
3 comments, last by peter86 18 years, 7 months ago
I tried to use a STL list with a struct but the compiler comes up with a lot of errors. Do I have to make an operator == for each type I'm gonna use or how do I get the compiler not to complain? See example app and build log below.

#include <list>

using namespace std;

struct MyStruct
{
	int i;
};

void main()
{
	list<MyStruct> lst;
	MyStruct data;

	data.i = 10;
	lst.push_back( data );

	lst.remove( data );
}

------ Build started: Project: test, Configuration: Debug Win32 ------ Compiling... main.cpp c:\Program\Microsoft Visual Studio .NET 2003\Vc7\include\list(682) : error C2784: 'bool std::operator ==(const std::list<_Ty,_Alloc> &,const std::list<_Ty,_Alloc> &)' : could not deduce template argument for 'const std::list<_Ty,_Ax> &' from 'std::allocator<_Ty>::value_type' with [ _Ty=MyStruct ] c:\Program\Microsoft Visual Studio .NET 2003\Vc7\include\list(976) : see declaration of 'std::operator`=='' c:\Program\Microsoft Visual Studio .NET 2003\Vc7\include\list(679) : while compiling class-template member function 'void std::list<_Ty>::remove(const _Ty &)' with [ _Ty=MyStruct ] c:\documents and settings\administratör\skrivbord\test\main.cpp(12) : see reference to class template instantiation 'std::list<_Ty>' being compiled with [ _Ty=MyStruct ] c:\Program\Microsoft Visual Studio .NET 2003\Vc7\include\list(682) : error C2784: 'bool std::operator ==(const std::allocator<_Ty> &,const std::allocator<_Other> &)' : could not deduce template argument for 'const std::allocator<_Ty> &' from 'std::allocator<_Ty>::value_type' with [ _Ty=MyStruct ] c:\Program\Microsoft Visual Studio .NET 2003\Vc7\include\xmemory(165) : see declaration of 'std::operator`=='' c:\Program\Microsoft Visual Studio .NET 2003\Vc7\include\list(682) : error C2784: 'bool std::operator ==(const std::istream_iterator<_Ty,_Elem,_Traits,_Diff> &,const std::istream_iterator<_Ty,_Elem,_Traits,_Diff> &)' : could not deduce template argument for 'const std::istream_iterator<_Ty,_Elem,_Traits,_Diff> &' from 'std::allocator<_Ty>::value_type' with [ _Ty=MyStruct ] c:\Program\Microsoft Visual Studio .NET 2003\Vc7\include\iterator(223) : see declaration of 'std::operator`=='' c:\Program\Microsoft Visual Studio .NET 2003\Vc7\include\list(682) : error C2784: 'bool std::operator ==(const std::istreambuf_iterator<_Elem,_Traits> &,const std::istreambuf_iterator<_Elem,_Traits> &)' : could not deduce template argument for 'const std::istreambuf_iterator<_Elem,_Traits> &' from 'std::allocator<_Ty>::value_type' with [ _Ty=MyStruct ] c:\Program\Microsoft Visual Studio .NET 2003\Vc7\include\xutility(940) : see declaration of 'std::operator`=='' c:\Program\Microsoft Visual Studio .NET 2003\Vc7\include\list(682) : error C2784: 'bool std::operator ==(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'std::allocator<_Ty>::value_type' with [ _Ty=MyStruct ] c:\Program\Microsoft Visual Studio .NET 2003\Vc7\include\xutility(641) : see declaration of 'std::operator`=='' c:\Program\Microsoft Visual Studio .NET 2003\Vc7\include\list(682) : error C2784: 'bool std::operator ==(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'std::allocator<_Ty>::value_type' with [ _Ty=MyStruct ] c:\Program\Microsoft Visual Studio .NET 2003\Vc7\include\utility(57) : see declaration of 'std::operator`=='' c:\Program\Microsoft Visual Studio .NET 2003\Vc7\include\list(682) : error C2676: binary '==' : 'std::allocator<_Ty>::value_type' does not define this operator or a conversion to a type acceptable to the predefined operator with [ _Ty=MyStruct ] Build log was saved at "file://c:\Documents and Settings\Administratör\Skrivbord\test\Debug\BuildLog.htm" test - 7 error(s), 0 warning(s) ---------------------- Done ---------------------- Thanks in advice, Peter
Advertisement
If you want to use list::remove() you'll need to have a comparison operator for your user defined type. If you don't use remove() for the user defined type then list won't require your class to define equality.
Quote:Original post by SiCrane
If you want to use list::remove() you'll need to have a comparison operator for your user defined type. If you don't use remove() for the user defined type then list won't require your class to define equality.


How else could I possibly remove an element from the list? Is there any way?
You can use iterators to go through the list and then use erase() instead.
Quote:Original post by SiCrane
You can use iterators to go through the list and then use erase() instead.


Okey, I'll try that. Thanks a lot!

This topic is closed to new replies.

Advertisement