Jump to content
  • Advertisement
Sign in to follow this  
peter86

STL list and user defined types

This topic is 4696 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!