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);
    }

You cannot instantiate an abstract class. You cannot instantiate an abstract class. You cannot instantiate an abstract class. You cannot instantiate an abstract class. You cannot instantiate an abstract class. You cannot instantiate an abstract class. You cannot instantiate an abstract class. You cannot instantiate an abstract class. You cannot instantiate an abstract class. You cannot instantiate an abstract class. You cannot instantiate an abstract class. You cannot instantiate an abstract class. You cannot instantiate an abstract class. You cannot instantiate an abstract class. You cannot instantiate an abstract class. You cannot instantiate an abstract class.

You should know that if you are using pure virtual functions.

Also I am query about your inheritance. I think using class composition is a better idea. Let sortStrategy compose all
of the sorting methods and don't let it be an abstract base class.

And why do you have the class word here anyway :

class sortStrategy* strategy;

Thats not doing what you think its doing.

When I just list it as

sortStrategy* strategy;

the compiler says that I must specify a type. I'm trying to set a private variable of class sortStrategy.

When I just list it as

sortStrategy* strategy;

the compiler says that I must specify a type. I'm trying to set a private variable of class sortStrategy.

You paid no attention to my last post.

This is you sortStrategy class :

class sortStrategy  {
      public:
 
      virtual sortStrategyOption sort(string students[100]) = 0;
 
};

It uses pure virtual function, ( the " = 0" part and the virtual part makes it pure virtual function). If a class has a pure virtual function,
it cannot instantiate an object of it! Remember that.

When you do this :

sortStrategy * strategy;

It is wrong. First that code does not instantiate an object yet,
because its a pointer to strategy. And when it does, it will be
an error because the sortStrategy class is an Abstract base class,
and that means no one can make on object of it. If you want
to make an object of it, then take out the " = 0 " part in your
sortStrategyOption function.

Thanks I don't have to have class in front of the private variables any longer after I took out the =0 part of the virtual function, however I am still getting the two origional errors with

strategy->sort(students);

I guess this is what happens when programming examples are done in class only on the board. A similar virtual function was on the board with

virtual BlackJackPlayOption play () = 0;

and then is called with

strategy->play();

I took C++ over a year ago, I guess when you don't program using it regularly you start to get a bit rusty.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.