-3

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?

supaplex
  • 157
  • 4
  • 18
  • Improve your original question please, or provide a bounty, but don't ask it again! – πάντα ῥεῖ Sep 26 '15 at 20:21
  • 1
    @πάνταῥεῖ But it is a different question? – supaplex Sep 26 '15 at 20:22
  • It's a different question, but still working on the same subject you have. We're not your personal help-desk here, to guide you through getting your problems solved, sorry. – πάντα ῥεῖ Sep 26 '15 at 20:24
  • 4
    @supaplex, the core problem is the linker error. There is a lot of code that has nothing to do with that error. – R Sahu Sep 26 '15 at 20:25
  • 1
    If you edit this question to refocus it on the link error it may encourage others to reopen it. You need to provide the compiler command you are using to build this project and perhaps change the title. @πάνταῥεῖ supaplex appears to have applied advice from the other question to solve that problem. Although this contains a **lot** of irrelevant code it is a different question. – Galik Sep 26 '15 at 20:33
  • @Galik I have edited the question according to your advices. – supaplex Sep 26 '15 at 20:39
  • @supaplex Well, here's a probably better duplicate, for what you're asking here: http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix – πάντα ῥεῖ Sep 26 '15 at 20:42
  • @supaplex Can you provide the whole console output from eclipse when you build this so we can see the actual link command? (make sure you try to build it once first so we don't get the compiling output, just the linking attempt). – Galik Sep 26 '15 at 20:45
  • @Galik question is updated. – supaplex Sep 26 '15 at 20:48
  • @supaplex I think you need to remove the declaration of static function `createFactory(FLIGHT_MODES)` from `class FlightModeInterface` because it doesn't have a definition. The one in `class FlightModeFactory` is **not** the same one. – Galik Sep 26 '15 at 20:55
  • @supaplex And in your `main()` call `FlightModeFactory::createFactory(FlightModeInterface::HOLDING);` instead (else move the function definition into `class FlightModeInterface`) – Galik Sep 26 '15 at 20:57
  • @supaplex Reopened the question now, in expectations that it will be closed again for the other dupe mentioned. – πάντα ῥεῖ Sep 26 '15 at 21:11

1 Answers1

0

Problem is solved when FlightModeFactory.cpp is replaced with FlightModeFactory.h.

supaplex
  • 157
  • 4
  • 18