I have a program where user enters the operations of a plane. User can select as many operations (holding, straight, landing etc.) as s/he wants. User can calculate the necessary fuel intake with operation 5.
I have applied to the Abstract Factory Design Pattern to the system as follows:
FlightModeInterface.h
class FlightModeInterface{
protected:
float time, fuel_rate, start, end, pace, distance;
float total;
public:
enum FLIGHT_MODES{
HOLDING,
RAISING,
LANDING,
STRAIGHT
};
FlightModeInterface();
virtual ~FlightModeInterface(){ }
virtual float calcFuel(float, float, float,
float, float, float) = 0;
static FlightModeInterface* createFactory(FLIGHT_MODES);
};
FlightModeFactory.cpp
#include "FlightModeInterface.h"
#include "Holding.h"
#include "Landing.h"
#include "Raising.h"
#include "Straight.h"
class FlightModeFactory{
protected:
float time, fuel_rate, start, end, pace, distance;
float total;
public:
static FlightModeInterface* createFactory(FlightModeInterface::FLIGHT_MODES mode){
switch (mode) {
case FlightModeInterface::HOLDING:
return new Holding();
case FlightModeInterface::LANDING:
return new Landing();
case FlightModeInterface::RAISING:
return new Raising();
case FlightModeInterface::STRAIGHT:
return new Straight();
}
throw "invalid flight mode.";
}
};
CalculateFuel.cpp
#include <iostream>
#include <stdio.h>
#include "FlightModeInterface.h"
using namespace std;
int main(){
char op = 's';
float time=0, fuel_rate=0, start=0, end=0, pace=0, distance=0;
float total = 0;
FlightModeInterface *factory;
while(op != 'x') {
float hold_result, raise_result, land_result, str_result;
cout << "Please select an operation: " << endl;
cout << "1 ---> Holding flight" << endl;
cout << "2 ---> Raising" << endl;
cout << "3 ---> Landing " << endl;
cout << "4 ---> Straight " << endl;
cout << "5 ---> Calculate total fuel consumption" << endl;
cout << "x ---> Exit " << endl;
cin >> op;
switch(op){
case '1':
cout << "Holding time (minutes): ";
cin >> time;
cout << "Fuel rate (kg/sec): ";
cin >> fuel_rate;
factory = FlightModeInterface::createFactory(FlightModeInterface::HOLDING);
hold_result = factory -> calcFuel(time, fuel_rate, 0, 0, 0, 0);
total += hold_result;
break;
case '5':
cout <<"Total fuel requirement: "<< total << " kg"<< endl;
total = 0;
break;
case 'x':
return 0;
default:
continue;
}
}
return 0;
}
When I run CalculateFuel.cpp, I encounter this error (I use Eclipse Builder to build the code on MAC):
23:46:08 **** Build of configuration Debug for project CalculateFuel ****
make all
Building file: ../src/CalculateFuel.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/CalculateFuel.d" -MT"src/CalculateFuel.d" -o "src/CalculateFuel.o" "../src/CalculateFuel.cpp"
Finished building: ../src/CalculateFuel.cpp
Building file: ../src/FlightModeFactory.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/FlightModeFactory.d" -MT"src/FlightModeFactory.d" -o "src/FlightModeFactory.o" "../src/FlightModeFactory.cpp"
Finished building: ../src/FlightModeFactory.cpp
Building target: CalculateFuel
Invoking: MacOS X C++ Linker
g++ -o "CalculateFuel" ./src/CalculateFuel.o ./src/FlightModeFactory.o
Undefined symbols for architecture x86_64:
"FlightModeInterface::createFactory(FlightModeInterface::FLIGHT_MODES)", referenced from:
_main in CalculateFuel.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [CalculateFuel] Error 1
Any ideas on how to solve this?