Hello everyone,
I'm trying my hand at the strategy design pattern. I've got most of my code working, however, I keep running into two errors
"invalid use of undefined type `struct sortStrategy' "
strategy->sort(students);
"forward declaration of `struct sortStrategy' "
strategy = s;}
I did a google search on both errors and it was suggested to do a forward declaration of my sortStrategy class, but that didn't solve the problem.
I've included my code below...thanks for the help!
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
enum sortStrategyOption {bubble, quick, merge, heap};
class ClassRecord {
public:
void buildClassBook (string students[100]) {
// do other things to prep class book
}
void sort(string students[]) {
// sort students alphabetically
sortStrategyOption options;
strategy->sort(students);
}
void setStrategy (class sortStrategy* s) {
strategy = s;}
private:
class sortStrategy* strategy;
};
class sortStrategy {
public:
virtual sortStrategyOption sort(string students[100]) = 0;
};
class BubbleSort: public sortStrategy {
public:
sortStrategyOption sort(string students[100]) {
//bubble sort
}
};
class QuickSort: public sortStrategy {
public:
sortStrategyOption sort(string students[100]){
//quick sort
}
};
class MergeSort: public sortStrategy {
public:
sortStrategyOption sort(string students[100]){
//merge sort
}
};
class HeapSort: public sortStrategy {
public:
sortStrategyOption sort(string students[100]){
//heap sort
}
};
int main () {
ClassRecord record;
string students [100] = {"Bill", "Jose", "Philipe", "Chaing", "Louise",
"Mary", "Lisa"};
record.buildClassBook(students);
}