Sorting list<class> elements - Function Object

Hi everyone.

I am try to sorting a custom class inside a list. I have already overloaded an operator() to sort according another criteria. Now that I have another set of data, in a list, of the same class I wish to sort them according another criteria and, for doing this, I have overloaded the same operator() but I get some errors:
Severity Code Source Description Project File Line
Error C2676 Build binary '-': 'std::_List_iterator<std::_List_val<std::_List_simple_types<Order>>>' does not define this operator or a conversion to a type acceptable to the predefined operator Orders c:\program files (x86)\microsoft visual studio 14.0\vc\include\algorithm 3203
Error C2676 Build binary '-': 'std::_List_iterator<std::_List_val<std::_List_simple_types<Order>>>' does not define this operator or a conversion to a type acceptable to the predefined operator Orders c:\program files (x86)\microsoft visual studio 14.0\vc\include\algorithm 3204
Error C2780 Build 'void std::_Sort(_RanIt,_RanIt,_Diff,_Pr)': expects 4 arguments - 3 provided Orders c:\program files (x86)\microsoft visual studio 14.0\vc\include\algorithm 3204
Error C2784 Build 'unknown-type std::operator -(std::move_iterator<_RanIt> &,const std::move_iterator<_RanIt2> &)': could not deduce template argument for 'std::move_iterator<_RanIt> &' from 'std::_List_iterator<std::_List_val<std::_List_simple_types<Order>>>' Orders c:\program files (x86)\microsoft visual studio 14.0\vc\include\algorithm 3203
Error C2784 Build 'unknown-type std::operator -(std::move_iterator<_RanIt> &,const std::move_iterator<_RanIt2> &)': could not deduce template argument for 'std::move_iterator<_RanIt> &' from 'std::_List_iterator<std::_List_val<std::_List_simple_types<Order>>>' Orders c:\program files (x86)\microsoft visual studio 14.0\vc\include\algorithm 3204
Error C2784 Build 'unknown-type std::operator -(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)': could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'std::_List_iterator<std::_List_val<std::_List_simple_types<Order>>>' Orders c:\program files (x86)\microsoft visual studio 14.0\vc\include\algorithm 3203
Error C2784 Build 'unknown-type std::operator -(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)': could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'std::_List_iterator<std::_List_val<std::_List_simple_types<Order>>>' Orders c:\program files (x86)\microsoft visual studio 14.0\vc\include\algorithm 3204

I guess it is because I have already overloaded that operator but, maybe I'm going to tell something stupid, shouldn't be the polymorphic rule according to a function declared after another declaration replaces the old one? It does not valid for operators?

Thanks!

Classes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class Order
{
protected:
	string name;
	string address;
	string data;
public:
	Order()
		: name{  }, address{  }, data{  } {};
	Order(string name1, string address1, string data1)
		: name{ name1 }, address{ address1 }, data{ data1 } {};
	~Order() {};
	string get_name() const { return name; };
	string get_address() const { return address; };
	string get_data() const { return data; };
	char get_delim() const { return ';'; };
};

class Purchase : Order
{
private:
	string product;
	double unit_price;
	int count;
public:
	Purchase()
		: product{  }, unit_price{ 0.0 }, count{ 0 } {};
	Purchase(string product1, double unit_price1, int count1)
		: product{ product1 }, unit_price{ unit_price1 }, count{ count1 } {};
	~Purchase() {};
	string get_product() const { return product; };
	double get_unitprice() const { return unit_price; };
	int get_count() const { return count; };
};

struct Sorting : Order
{
	bool operator() (const Order& o1, const Order& o2)
	{
		return o1.get_name() < o2.get_name();
	}
};

struct Sorting_Addr : Order
{
	bool operator() (const Order& o1, const Order& o2)
	{
		return o1.get_address() < o2.get_address();
	}
};


Source:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void sort_vec(vector<Order>& o)
{
	system("CLS");
	cout << "Sorted Items: " << endl;
	sort(o.begin(), o.end(), Sorting());
	orders_vec_print(o);
}

void sort_list(list<Order>& l)
{
	system("CLS");
	cout << "Sorted Items: " << endl;
	sort(l.begin(), l.end(), Sorting_Addr());
	orders_list_print(l);
}
Last edited on
Why do Sorting and Sorting_Addr inherit from Order? You shouldn't be doing that.

The issue is that you accidentally used std::list instead of std::vector - just use std::vector instead and the problem should go away.
Last edited on
It was for an homework I am doing!

Oh ok I thought that in this case inheritance was allowable.

I have found the bug: I used std::sort instead of std::list::sort.

Now it works! I will follow your advice and get inheritance out of the parent class.

Thanks!
Topic archived. No new replies allowed.