Accessing member parent function to child

The problem is I have a Sort parent class (Sorter) and many children( merge,bubble,quick)

What I m trying to do is to access the function "runOnce" which is a function in parent class.

Main.cpp

int main()
{
    fstream Data;
    Data.open("Test.txt",ios::in|ios::out);

    Sorter<int>* bubble = new BubbleSort<int>;
      Sorter<int>* quick = new QuickSort<int> ;
    Sorter<int>* merge = new MergeSort<int> ;
    vector<int> list;
     while(!Data.eof())
    {
      int temp; 
     Data >> temp;
      list.push_back(temp);
    }
    Sorter<int>::runOnce(bubble, list);
    Data.close();
}

Sorter.cpp

#ifndef SORTER_H
#define SORTER_H
#include <vector>
using namespace std;

template <class T>
class Sorter {
    public:
        Sorter(){};
        virtual void sort(vector<T>&, int, int) = 0;
        double runOnce(Sorter<T>&, vector<T>&);

};

#endif // SORTER_H

but I m getting an error in main in line of

Sorter::runOnce(bubble, list);

it says:

no matching function for call to 'sorter:: runOnce(Sorter*&, std:: vector < int , std :: allocator < int > > &)

And I tried to change it to be

bubble->Sorter->runOnce(bubble, list);

and it said

invalid use of Sorter< int >::Sorter

How can I fix this?

1 answer

  • answered 2018-04-14 15:09 Janne Husberg

    The function takes a Sorter&, not a Sorter*&. Dereference the pointer when passing it in, e.g: Sorter<int>::runOnce(*bubble, list);