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>::sor t_child' to create a pointer to member c:\users\owner\documents\v isual 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\v isual studio 2010\projects\4heap\4heap\ pq.h 107 1 4Heap
This is the line that is causing the problem
PQ.cpp
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
Error 3 error C2780: 'void std::sort(_RanIt,_RanIt)' : expects 2 arguments - 3 provided c:\users\owner\documents\v
This is the line that is causing the problem
sort(vec_child.begin(), vec_child.end(), sort_child);
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.