samedi 26 septembre 2015

Abstract Factory Pattern Error with C++ [duplicate]

This question is an exact duplicate of:

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

Holding.h

#include "FlightModeInterface.h"

class Holding: public FlightModeInterface{
public:

    Holding()
            :FlightModeInterface(){ };
    virtual ~Holding(){};

    virtual float calcFuel(float, float, float,
              float, float, float){
        total = (time * fuel_rate * 60);
        return total;
    }
};

Landing.h

#include "FlightModeInterface.h"

class Landing: public FlightModeInterface{
public:

    Landing()
            :FlightModeInterface(){ };

    virtual ~Landing(){};

    virtual float calcFuel (float, float, float,
              float, float, float){
        if(start > end && pace != 0 ){
            float landing_time = (start - end)/pace;
            total =  landing_time * fuel_rate;
            return total;
        }else{
            return 0;
        }
    }
};

Raising.h

#include "FlightModeInterface.h"

class Raising: public FlightModeInterface{
public:

    Raising()
            :FlightModeInterface(){ };

    virtual ~Raising(){};

    virtual float calcFuel (float, float, float,
              float, float, float){

        if(start < end && pace != 0 ){
            float rising_time = (end - start)/pace;
            total = rising_time * fuel_rate;
            return total;
        }else{
            return 0;
        }
    }
};

Straight.h

#include "FlightModeInterface.h"

class Straight: public FlightModeInterface{
public:

    Straight()
            :FlightModeInterface(){ };

    virtual ~Straight(){};

    virtual float calcFuel (float, float, float,
              float, float, float){
        if(distance != 0 || pace != 0 ){
            float straight_time = distance/pace;
            total = straight_time * fuel_rate;
            return total;
        }else{
            return 0;
        }
    }
};

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 '2':
            cout << "Enter starting altitude of raising (meters): ";
            cin >> start;
            cout << "Enter ending altitude of raising (meters):";
            cin >> end;
            cout << "Enter raising pace (meter/sec): ";
            cin >> pace;
            cout << "Fuel rate (kg/sec): ";
            cin >> fuel_rate;

            factory = FlightModeInterface::createFactory(FlightModeInterface::RAISING);

            raise_result = factory -> calcFuel(0, fuel_rate, start, end, pace, 0);

            total += raise_result;
            break;
        case '3':
            cout << "Enter starting altitude of landing (meters): ";
            cin >> start;
            cout << "Enter ending altitude of landing (meters):  ";
            cin >> end;
            cout << "Enter landing pace (meter/sec):  ";
            cin >> pace;
            cout << "Fuel rate (kg/sec):  ";
            cin >> fuel_rate;

            factory = FlightModeInterface::createFactory(FlightModeInterface::LANDING);

            land_result = factory -> calcFuel(0, fuel_rate, start, end, pace, 0);

            total += land_result;
            break;
        case '4':
            cout << "Enter distance for straight flight (meters): ";
            cin >> distance;
            cout << "Enter flight pace (meter/sec): ";
            cin >> pace;
            cout << "Fuel rate (kg/sec): ";
            cin >> fuel_rate;

            factory = FlightModeInterface::createFactory(FlightModeInterface::STRAIGHT);

            str_result = factory -> calcFuel(0, fuel_rate, 0, 0, pace, distance);

            total += str_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:

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)

Any ideas on how to solve this?

Aucun commentaire:

Enregistrer un commentaire