C++ "undefined reference to"错误
C++ "undefined reference to" error
首先不要惊慌。这是一个非常简单的程序。我在用"g++ -Wall -pedantic Main.cpp"编译Main.cpp时得到这个错误这是我所有的文件。是什么导致未定义的引用出错?
Main.cpp
#include <iostream>
#include "BMWLogo.h"
#include "Engine.h"
#include "IVehicle.h"
#include "Car.h"
#include "BMW.h"
int main() {
BMW* bmw = new BMW();
Car* car = bmw;
std::cout << car->getName() << std::endl;
}
IVehicle.h
class IVehicle {
public:
IVehicle();
virtual std::string getName();
virtual float getCurrentSpeed();
};
IVehicle.cpp
#include "IVehicle.h"
IVehicle::IVehicle() {
}
virtual std::string IVehicle::getName() {
}
virtual float IVehicle::getCurrentSpeed() {
}
Car.h
class Car : public IVehicle {
private:
std::string name;
float currentSpeed;
Engine* engine;
public:
Car(std::string name);
void setCurrentSpeed(float currentSpeed);
float getCurrentSpeed();
std::string getName();
};
Car.cpp
#include "Car.h"
Car::Car(std::string name) {
this->name = name;
engine = new Engine();
}
void Car::setCurrentSpeed(float currentSpeed) {
this->currentSpeed = currentSpeed;
}
float Car::getCurrentSpeed() {
return currentSpeed;
}
std::string Car::getName() {
return name;
}
BMW.h
class BMW : public Car {
private:
BMWLogo* bmwLogo;
public:
BMW();
};
BMW.cpp
#include "BMW.h"
BMW::BMW()
: Car("BMW") {
bmwLogo = new BMWLogo();
}
Engine.h
class Engine {
Engine();
};
Engine.cpp
#include "Engine.h"
Engine::Engine() {
}
BMWLogo.h
class BMWLogo {
BMWLogo();
};
BMWLogo.cpp
#include "BMWLogo.h"
BMLogo::BMWLogo() {
}
您错过了IVehicle
构造函数的定义。
乍一看,我认为IVehicle.h需要在Car.h中引用
#include "IVehicle.h"
这是一个不同于你所问的问题,但你可能想要注意它后面,看你的代码:
Car::Car(std::string name) {
name = name;
engine = new Engine();
}
您可能希望更改参数名称,以便它不会隐藏name的类实例。试一试:
Car::Car(std::string p_name) {
name = p_name;
engine = new Engine();
}
相关文章:
- "error: no matching function for call to"构造函数错误
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 使用 MATLAB 编码器生成C++代码:编译错误"undefined reference to `rgb2gray_tbb_real64'"
- 尝试链接我的着色器时,我收到错误代码"error c5145 must write to gl_position"
- 为什么创建友元类的实例会导致"undefined reference to"错误?
- 使用 cmake 的 Linux 终端上的"Conversion to non-scalar type is requested"错误
- "no matching function for call to 'Vector::Vector'"错误
- C++:如何将 unix 时间的字符串转换为 *tm?(使用时间错误:"cannot convert 'String' to 'tm*' ")
- 我在 ifstream input_file(文件名)行中收到错误"no matching function to call";
- Win32 API - HWND "{unused = ???} Unable to read memory"错误
- 尝试使用 extern "C" 调用 C 中的C++方法,得到"undefined reference to"对象的链接器错误
- 结构错误:"no matching function for call to "
- 我一直收到错误"cannot convert 'float*' to 'float' in return"
- 如果子类中没有构造函数方法,则错误"no matching function for call to 'LGame::LGame(String&)'"
- 从生成文件编译错误:"Unable to open output file" ..."No such file or directory"
- 与映射和unordered_map相关的编译错误:"attempting to reference a deleted function"
- 索引 2D 向量时'Reference to type const..'编译错误
- 如何修复以下错误"no match for call to '(std::tr1::shared_ptr<_iobuf*>) (FILE*&)'"
- 为什么我收到以下错误:"returning reference to temporary [-Werror=return-local-addr]"
- 为什么我会收到"invalid conversion from 'Queue*/Stack*' to 'int'"错误消息?