c++ - 创建指向类的指针时对类和函数的未定义引用。项目是用 CMake 和 g++ 构建的
问题描述
我有一个项目,我使用 cmake 和命令行 g++ main.cpp -o main 构建它们都返回错误:未定义的引用但指向不同的对象。
汽车.h
#include "upgrade.h"
#include "Usage.h"
class Car { // abstract class
public:
Car();
virtual ~Car();
void getProperty(); // test void function
virtual int run ( int &mode ); // test function return value, and receives pass by reference parameter
virtual int fuelType ( int &type ); // test function return value, and receives pass by pointer
virtual int upgrade ( Upgrade *upgrade); // test function accepting a class pointer as parameter;
virtual int specialUse ( Usage use); // test function accepting a class object as parameter;
virtual int multiTask ( int speed); // test function running on multi-thread
virtual int maintain ( int run); // test function returning error code in errono
static int carBasicProperty;
private:
char * model [100]; // test per character manipulation
int mode; // car runs and sets run mode as normal or sport
};
汽车.cpp
#include "car.h"
Car::Car(){};
virtual Car:: ~Car(){};
virtual void Car::getProperty(){};
virtual int Car::run ( int &mode ){return 0;};
virtual int Car::fuelType ( int &type ){return 0;};
virtual int Car::upgrade ( Upgrade *upgrade){return ;};
virtual int Car::specialUse ( Usage use){return 0;};
virtual int Car::multiTask ( int speed){return 0;};
virtual int Car::maintain ( int run){return 0;};
模型X.h
#include "car.h"
class modelX : public Car{
public:
modelX(char* customer); // test parameterized construtor
virtual ~modelX(); // unittest requires virtual for destructor
void setProperty(int cost, int mode, int fuelType, int upgrade, int usage); // test void* function
void getProperty(); // test modifying function and test
virtual int run ( int &mode , int distance); // function overloaded the parent function
virtual int fuelType ( int *type ); // test function return value, and receives pass by pointer
virtual int upgrade ( Upgrade *upgrade); // test function accepting a class pointer as parameter;
virtual int specialUse ( Usage use); // test function accepting a class object as parameter;
virtual int multiTask ( int speed); // test function running on multi-thread
virtual int maintain ( int run); // test function returning error code in errono
private:
int m_cost;
int m_mode;
int m_fuelType;
char * m_customer;
};
模型X.cpp
# include "modelX.h"
# define NORMAL 100
# define SPORT 200
# define ECO 300
# define HYBRID 1
# define ELECTRICAL 2
# define GAS 3
modelX::modelX(char* customer): m_customer(customer){} ;
modelX:: ~modelX(){};
void modelX::getProperty(){};
void modelX::setProperty(int cost, int mode, int fuelType, int upgrade, int usage){
m_cost = cost;
m_mode = mode;
m_fuelType = fuelType;
};
int modelX::run ( int &mode , int distance){
switch (mode){
case NORMAL:
{
int fuelCost = 10;
for (int i = 1; i < distance; i++){
int fuelCost = +10;
};
return fuelCost;
};
case SPORT:
{
int fuelCost = 10;
for (int i = 1; i < distance; i++){
int fuelCost = +20;
};
return fuelCost;
};
case ECO:
{
int fuelCost = 10;
for (int i = 1; i < distance; i++){
int fuelCost = +5;
};
return fuelCost;
};
default:
return -1;
}
};
int modelX::fuelType ( int *type ){
return 0;
};
int modelX::upgrade ( Upgrade *upgrade){
return 0;
};
int modelX::specialUse ( Usage use){
return 0;
};
int modelX::multiTask ( int speed){
return 0;
};
int modelX::maintain ( int run){
return 0;
};
所以基本上只是一个父类和一个子类。我把所有的源代码放在一个文件夹里,还有 CMakeLists.txt 在那里。
CMakeLists.txt
cmake_minimum_required (VERSION 3.10)
project (CAR VERSION 1.0)
#set build options or set C++ standard
set(CMAKE_CXX_STANDARD 11)
#set include paths (if sources has more subfolders)
include_directories(
${PROJECT_SOURCE_DIR}/include
)
#add executable target name
add_executable(${PROJECT_NAME} main.cpp modelX.cpp upgrade.cpp usage.cpp)
如果我输入 g++ main.cpp -o main,我有这个错误:
/usr/bin/ld: /tmp/ccrKjNks.o: in function `main':
main.cpp:(.text+0x32): undefined reference to `modelX::modelX(char*)'
collect2: error: ld returned 1 exit status
如果我输入 cmake --build 。在构建文件夹中,然后我有以下错误:
/usr/bin/ld: CMakeFiles/CAR.dir/modelX.cpp.o: in function `modelX::modelX(char*)':
modelX.cpp:(.text+0x1c): undefined reference to `Car::Car()'
/usr/bin/ld: CMakeFiles/CAR.dir/modelX.cpp.o: in function `modelX::~modelX()':
modelX.cpp:(.text+0x66): undefined reference to `Car::~Car()'
/usr/bin/ld: CMakeFiles/CAR.dir/modelX.cpp.o:(.data.rel.ro._ZTV6modelX[_ZTV6modelX]+0x20): undefined reference to `Car::run(int&)'
/usr/bin/ld: CMakeFiles/CAR.dir/modelX.cpp.o:(.data.rel.ro._ZTV6modelX[_ZTV6modelX]+0x28): undefined reference to `Car::fuelType(int&)'
/usr/bin/ld: CMakeFiles/CAR.dir/modelX.cpp.o:(.data.rel.ro._ZTI6modelX[_ZTI6modelX]+0x10): undefined reference to `typeinfo for Car'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/CAR.dir/build.make:145: CAR] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/CAR.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
1/是什么导致了这些错误?2/为什么 cmake 和 g++ 返回不同的错误?
解决方案
推荐阅读
- arrays - 使用对象中使用的数组计算总和
- c# - 在 ml.net 上制作预测引擎时在同一行上转换数据
- javascript - DiscordJS Bot - 如何在频道中的另一条消息之前立即获取消息?
- javascript - 如何为 JavaScript 数组中的对象添加值?
- python - 使用 Selenium 和 Python 进行网络抓取时出现问题
- python - python中set()中的花括号和方括号
- django - 解决 ConnectionAbortedError: [WinError 10053] 而不会弄乱 Windows Defender 和防火墙
- java - 如何修复我的扫描仪错误?java.util.InputMismatchException
- javascript - checkall / un-checkall check boxes with js
- javascript - 如何更改 Yup/Formik 中的默认错误文本?