Hello,
I have a simple program pasted below in which I am trying to sort a list
containing custom objects. The std::sort uses a compare function to sort the
list.
The following is the list of errors that I receive when I am trying to
compile the code. I am unable to find the problem. Please let me know if you
know whats wrong.
Thanks,
Rishabh Ghia.
--------------------Configuration: Cpp1 - Win32 Debug--------------------
Compiling...
Cpp1.cpp
c:\program files\microsoft visual studio\vc98\include\algorithm(592) : error
C2784: '_D __cdecl std::operator -(const class
std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &,const class
std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &)' : could not deduce temp
late argument for 'const class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &'
from 'class std::list<class Dog,class std::allocator<class Dog> >::iterator'
c:\program files\microsoft visual studio\vc98\include\algorithm(589)
: see reference to function template instantiation 'void __cdecl
std::_Sort_0(class std::list<class Dog,class std::allocator<class Dog>
>::iterator,class std::list<class Do
g,class std::allocator<class Dog> >::iterator,class Dog::Mysort,class Dog
*)' being compiled
c:\program files\microsoft visual studio\vc98\include\algorithm(592) : error
C2784: '_D __cdecl std::operator -(const class
std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &,const class
std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &)' : could not deduce temp
late argument for 'const class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &'
from 'class std::list<class Dog,class std::allocator<class Dog> >::iterator'
c:\program files\microsoft visual studio\vc98\include\algorithm(589)
: see reference to function template instantiation 'void __cdecl
std::_Sort_0(class std::list<class Dog,class std::allocator<class Dog>
>::iterator,class std::list<class Do
g,class std::allocator<class Dog> >::iterator,class Dog::Mysort,class Dog
*)' being compiled
c:\program files\microsoft visual studio\vc98\include\algorithm(592) : error
C2784: '_D __cdecl std::operator -(const class
std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &,const class
std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &)' : could not deduce temp
late argument for 'const class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &'
from 'class std::list<class Dog,class std::allocator<class Dog> >::iterator'
c:\program files\microsoft visual studio\vc98\include\algorithm(589)
: see reference to function template instantiation 'void __cdecl
std::_Sort_0(class std::list<class Dog,class std::allocator<class Dog>
>::iterator,class std::list<class Do
g,class std::allocator<class Dog> >::iterator,class Dog::Mysort,class Dog
*)' being compiled
c:\program files\microsoft visual studio\vc98\include\algorithm(592) : error
C2784: '_D __cdecl std::operator -(const class
std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &,const class
std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &)' : could not deduce temp
late argument for 'const class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &'
from 'class std::list<class Dog,class std::allocator<class Dog> >::iterator'
c:\program files\microsoft visual studio\vc98\include\algorithm(589)
: see reference to function template instantiation 'void __cdecl
std::_Sort_0(class std::list<class Dog,class std::allocator<class Dog>
>::iterator,class std::list<class Do
g,class std::allocator<class Dog> >::iterator,class Dog::Mysort,class Dog
*)' being compiled
c:\program files\microsoft visual studio\vc98\include\algorithm(592) : error
C2676: binary '-' : 'class std::list<class Dog,class std::allocator<class
Dog> >::iterator' does not define this operator or a conversion to a type
acceptable to the predef
ined operator
c:\program files\microsoft visual studio\vc98\include\algorithm(589)
: see reference to function template instantiation 'void __cdecl
std::_Sort_0(class std::list<class Dog,class std::allocator<class Dog>
>::iterator,class std::list<class Do
g,class std::allocator<class Dog> >::iterator,class Dog::Mysort,class Dog
*)' being compiled
c:\program files\microsoft visual studio\vc98\include\algorithm(596) : error
C2784: 'class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> __cdecl
std::operator +(_D,const class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &)'
: could not deduce template arg
ument for '' from 'class std::list<class Dog,class std::allocator<class Dog>
>::iterator'
c:\program files\microsoft visual studio\vc98\include\algorithm(589)
: see reference to function template instantiation 'void __cdecl
std::_Sort_0(class std::list<class Dog,class std::allocator<class Dog>
>::iterator,class std::list<class Do
g,class std::allocator<class Dog> >::iterator,class Dog::Mysort,class Dog
*)' being compiled
c:\program files\microsoft visual studio\vc98\include\algorithm(596) : error
C2676: binary '+' : 'class std::list<class Dog,class std::allocator<class
Dog> >::iterator' does not define this operator or a conversion to a type
acceptable to the predef
ined operator
c:\program files\microsoft visual studio\vc98\include\algorithm(589)
: see reference to function template instantiation 'void __cdecl
std::_Sort_0(class std::list<class Dog,class std::allocator<class Dog>
>::iterator,class std::list<class Do
g,class std::allocator<class Dog> >::iterator,class Dog::Mysort,class Dog
*)' being compiled
c:\program files\microsoft visual studio\vc98\include\algorithm(596) : error
C2780: 'void __cdecl std::_Insertion_sort(_RI,_RI)' : expects 2 arguments -
3 provided
c:\program files\microsoft visual studio\vc98\include\algorithm(569)
: see declaration of '_Insertion_sort'
c:\program files\microsoft visual studio\vc98\include\algorithm(589)
: see reference to function template instantiation 'void __cdecl
std::_Sort_0(class std::list<class Dog,class std::allocator<class Dog>
>::iterator,class std::list<class Do
g,class std::allocator<class Dog> >::iterator,class Dog::Mysort,class Dog
*)' being compiled
c:\program files\microsoft visual studio\vc98\include\algorithm(597) : error
C2676: binary '+=' : 'class std::list<class Dog,class std::allocator<class
Dog> >::iterator' does not define this operator or a conversion to a type
acceptable to the prede
fined operator
c:\program files\microsoft visual studio\vc98\include\algorithm(589)
: see reference to function template instantiation 'void __cdecl
std::_Sort_0(class std::list<class Dog,class std::allocator<class Dog>
>::iterator,class std::list<class Do
g,class std::allocator<class Dog> >::iterator,class Dog::Mysort,class Dog
*)' being compiled
Error executing cl.exe.
Cpp1.obj - 9 error(s), 0 warning(s)
Source Code.
#include<iostream.h>
#include <list>
#include <algorithm>
using namespace std;
class Dog
{
public:
int m_nAge;
Dog(int a_nAge):m_nAge(a_nAge)
{
// Class Constructor
}
void Print()
{
cout << m_nAge << endl;
}
int operator - (const Dog& d1)
{
return m_nAge - d1.m_nAge;
}
class Mysort
{
public:
bool operator()(const Dog d1, const Dog d2)
{
return d1.m_nAge > d2.m_nAge;
}
};
};
void main()
{
std::list<Dog> _list1;
std::list<Dog>::iterator _iterator;
int nCtr = 0;
for (nCtr=5;nCtr>=0;nCtr--)
{
Dog d1(nCtr);
_list1.push_back(d1);
}
//printing list
for (_iterator = _list1.begin();_iterator != _list1.end();_iterator++)
{
_iterator->Print();
}
// Sorting by age
std::sort(_list1.begin(),_list1.end(),Dog::Mysort());
};