c++ - invalid new-expression of abstract class type error -
homework
i'm getting type of error merge sort function. think error has merge()
method, thought took care of using in mergesort()
. appreciated.
sort.h
#ifndef sort_h_ #define sort_h_ class sort { protected: unsigned long num_cmps; // number of comparisons performed in sort function public: virtual void sort(int a[], int size)= 0; // main entry point bool testifsorted(int a[], int size); // returns false if not sorted // true otherwise unsigned long getnumcmps() { return num_cmps; } // returns # of comparisons void resetnumcmps() { num_cmps = 0; } }; class mergesort:public sort { // mergesort class public: void sort(int a[], int size, int low, int high); // main entry point }; #endif
sort.cpp
sort* s; switch(op.getalg()) { case 's': s=new selectionsort(); break; case 'i': s=new insertionsort(); break; case 'b': s=new bubblesort(); break; case 'h': s=new heapsort(); break; case 'r': s=new radixsort(); radixsortq = true; break; case 'm': s=new mergesort(); --> error break; }
merge sort.cpp
#include <iostream> #include "sort.h" using namespace std; void mergesort::sort(int a[], int size, int low, int high) // main entry point { if (low < high){ int middle = (low + high) / 2; mergesort(a, size, low, middle); mergesort(a, size, middle+1, high); merge(a, size, low, middle, high); } } void merge::sort(int a[], int size, int low, int middle, int high){ int temp[size]; for(int = low; <= high; i++){ temp[i] = a[i]; } int = low; int j = middle + 1; int k = low; while(i <= middle && j <= high){ if(temp[i] <= temp[j]){ a[k] = temp[i]; ++i; } else { a[k] = temp[j]; ++j; } ++k; } while(i <= middle){ a[k] = temp[i]; ++k; ++i; } }
you aren't overriding sort function, mergesort still abstract class:
sort::sort(int a[], int size)= 0; mergesort::sort(int a[], int size, int low, int high); // main entry point
these sorts have different signatures, different functions.
to solve this, either need change mergesort::sort method not take in low
, high
argument, or add low
, high
argument other sort methods.