Link to home
Start Free TrialLog in
Avatar of brich744
brich744

asked on

Error with STL Sort

Hello Everyone,

I am having an issue sorting my vector of objects.  I am using the stl sort algorithm but I am getting the following errors:

Error      2      error C3867: 'PQ<Comparable,Data>::sort_child': function call missing argument list; use '&PQ<Comparable,Data>::sort_child' to create a pointer to member      c:\users\owner\documents\visual studio 2010\projects\4heap\4heap\pq.h      107      1      4Heap

Error      3      error C2780: 'void std::sort(_RanIt,_RanIt)' : expects 2 arguments - 3 provided      c:\users\owner\documents\visual studio 2010\projects\4heap\4heap\pq.h      107      1      4Heap

This is the line that is causing the problem
 
sort(vec_child.begin(), vec_child.end(), sort_child);

Open in new window


PQ.cpp
#ifndef PQ_H
#define PQ_H

#include<iostream>
#include<vector>
#include<cmath>
#include<algorithm>

using namespace std;

#include"StackLList.h"

template<class Data> class ListNode;

template<class Comparable, class Data>
class PQ
{
public:
	PQ();
	void insert(ListNode<Data>&);
	void delete_max();
	void swap(ListNode<Data>&, ListNode<Data>&);
	int max_child(int&);
	bool sort_child(const ListNode<Data>&, const ListNode<Data>&);

private:
	vector< ListNode<Data> > vector_PQ;
	int size;
};

template<class Comparable, class Data>
PQ<Comparable, Data>::PQ()
	:size(0)
{}

template<class Comparable, class Data>
void PQ<Comparable, Data>::insert(ListNode<Data> &x)
{
	if(vector_PQ.size() == 0)
	{
		vector_PQ.push_back(x);
		size++;
		return;
	}
	
		size++;
		vector_PQ.push_back(x);
		int child = vector_PQ.size()-1;
	int parent = (child/4);

	while(vector_PQ[parent].counter < vector_PQ[child].counter)
	{
		swap(vector_PQ[parent], vector_PQ[child]);
		child = parent;
		parent = (child/4);
	}
}

template<class Comparable, class Data>
void PQ<Comparable, Data>::delete_max()
{

	if(vector_PQ.size() == 1)
	{
		vector_PQ.pop_back();
		return;
	}
	ListNode<Data> nextItem = vector_PQ[0];
	swap(vector_PQ[0], vector_PQ[size-1]);
	size--;

	vector_PQ.pop_back();

	int parent = 0;
	int max = max_child(parent);

	while(vector_PQ[parent].counter < vector_PQ[max].counter)
	{
		parent = max;
		max_child(parent);
	}
	
	
	
}

template<class Comparable, class Data>
int PQ<Comparable, Data>::max_child(int& parent)
{
	vector< ListNode<Data> > vec_child;
	int iter = 1;

	for(int i = 4; i>= 1; i--)
	{
		
		int child = (4*parent)+iter;

		if(child <= vector_PQ.size() - 1)
			{
				vec_child.push_back(vector_PQ[child]);
			}
	
		iter--;

	}

	sort(vec_child.begin(), vec_child.end(), sort_child);


	return parent;

}

template<class Comparable, class Data>
bool PQ<Comparable,Data>::sort_child(const ListNode<Data>& a, const ListNode<Data>& b)
{

	return a.counter <= b.counter;

}
template<class Comparable, class Data>
void PQ<Comparable, Data>::swap(ListNode<Data>& a, ListNode<Data>& b)
{
	ListNode<Data> temp;

	temp = a;
	a = b;
	b = temp;
}

#endif

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Todd Gerbert
Todd Gerbert
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial