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());

};

Re: Compilation Error while sorting a list by Tim

Tim
Sun Apr 25 12:15:51 CDT 2004

ghia wrote:
> 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.
[...]

std::sort requires random-access iterators, whereas list only supplies
bidirectional iterators. For example, if you replace 'vector' in your code
with 'list', it works correctly.

A vector's iterators allow access to any point in the vector, whereas a
list's iterators only allow access to the item immediately before or after.
std::sort requires access to arbitrary items within the range you give it.

--
Tim Robinson (MVP, Windows SDK)
http://mobius.sourceforge.net/



Re: Compilation Error while sorting a list by Jerry

Jerry
Sun Apr 25 12:22:16 CDT 2004

In article <#05zSNuKEHA.3324@TK2MSFTNGP10.phx.gbl>, ghia@hathway.com
says...
> 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.

You can't use std::sort on an std::list -- you have to use
std::list::sort instead.

With that said, my advice would be against using std::list _or_
std::sort. At least for what you're showing below, an std::multiset
will do the job much more cleanly:

[ ... ]

> class Dog
> {
> public:
> int m_nAge;

Bad dog! Go outside until you learn to keep your member private! :-)

I'd write the code something like this:

#include <iostream>
#include <set>

class Dog {
int age_;
public:
Dog(int age) : age_(age) {}
friend std::ostream &operator<<(std::ostream &, Dog const &);
friend struct age_order;
};

struct age_order {
bool operator()(Dog const &a, Dog const &b) {
return a.age_ < b.age_;
}
};

std::ostream &operator<<(std::ostream &os, Dog const &d) {
return os << d.age_;
}

int main() {
std::multiset<Dog, age_order> dogs;

// create the dogs with random ages so they don't start out
// sorted...
for (int I=0; I<5; I++)
dogs.insert(Dog(rand()));

// but now, by the magic of the multiset, they ARE sorted.
std::copy(dogs.begin(), dogs.end(),
std::ostream_iterator<Dog>(std::cout, "\n"));
return 0;
}

IMO, std::list should be treated almost like a goto -- the number of
jobs for which it's the right tool is exceedingly small (basically
restricted to situations where there is a strong concept of a
"current point" in the list). For most jobs, other containers will
work better.

In fact, depending on usage, an std::multiset might not be the ideal
for this job either -- if you ONLY want to read in some data, sort
it, and then print it out, chances are an std::vector is better
still. An std::multiset has one nice property compared to an
std::vector: inserting and removing items anywhere in the set is easy
and never disturbs its overall order.

--
Later,
Jerry.

The universe is a figment of its own imagination.