[英]Undefined symbols for architecture x86_64 Linker Error with C++
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.用户可以通过操作 5 计算所需的燃料摄入量。
I have applied to the Abstract Factory Design Pattern to the system as follows:我已将抽象工厂设计模式应用于系统,如下所示:
FlightModeInterface.h飞行模式接口.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飞行模式工厂.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计算燃料.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):当我运行 CalculateFuel.cpp 时,我遇到了这个错误(我使用 Eclipse Builder 在 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?关于如何解决这个问题的任何想法?
FlightModeFactory.cpp 替换为 FlightModeFactory.h 后问题解决。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.