编译多源文件时未解决的外部问题
Unresolved external issue when compiling multi-source files
我有一个项目,由两个CPP文件(main.CPP和Car.CPP)和一个头文件(Car.h)组成。该程序旨在允许用户输入汽车的型号、品牌和速度,并显示修改后的速度。我的问题是,当我编译项目时,我收到一个"1个未解决的外部"问题,如下所示:
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall Car::Car(void)" (??0Car@@QAE@XZ) referenced in function _main
1>C:UsersShaidiDesktopClassesCIST 2362ProjectsmainDebugmain.exe : fatal error LNK1120: 1 unresolved externals
这是主.cpp文件:
// main.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Car.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
string make;
int model, speed;
Car c;
//user input and assignment for make, model, and speed
cout << "Enter the make of the car: " <<endl;
cin >> make;
c.setMake(make);
cout << "Enter the model of the car: " <<endl;
cin >> model;
c.setYearModel(model);
cout << "Enter the speed of the car: " <<endl;
cin >> speed;
c.setSpeed(speed);
//print make and model
cout << "Car make: " << c.getMake() <<endl;
cout << "Car model: " << c.getYearModel() << endl;
cout << "Car speed: " << c.getSpeed() <<endl;
//loops to calculate and print acceleration and braking
for (int i = 0; i < 5; i ++){
cout << "Car speed after acceleration: " <<c.accelerate() <<endl;
}
for (int i = 0; i < 5; i ++){
cout << "Car speed after braking: " <<c.brake() <<endl;
}
return 0;
} //end main
这是Car.cpp文件:
// Car.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Car.h"
#include <cstdlib>
#include <string>
#include <iostream>
using namespace std;
Car::Car(int y, string m)
{
string make = m;
int year = y;
speed = 0;
}
void Car::setYearModel(int y)
{
yearModel = y;
}
void Car::setSpeed(int s)
{
if (s >= 0){
speed = s;
} else {
cout << "Invalid speed";
exit(EXIT_FAILURE);
}
}
void Car::setMake(string m)
{
make = m;
}
int Car::getYearModel()
{
return yearModel;
}
int Car::getSpeed()
{
return speed;
}
string Car::getMake()
{
return make;
}
int Car::accelerate()
{
return speed + 5;
}
int Car::brake()
{
return speed - 5;
}
这是Car.h文件:
#ifndef CAR_H
#define CAR_H
#include <string>
using namespace std;
class Car
{
private:
std::string make;
int yearModel;
int speed;
public:
Car();
Car(int, std::string);
void setYearModel(int);
void setSpeed(int);
void setMake(std::string);
int getYearModel() ;
int getSpeed() ;
int accelerate() ;
int brake() ;
std::string getMake() ;
};
#endif // CAR_H
您错过了Car()
默认构造函数的实现。
class Car
{
public:
// There is no implementation.
Car();
}
您已经声明了Car::Car()
,但从未定义过它。可以向.cpp文件添加定义,也可以从标头中删除声明。
例如:
Car::Car()
{
}
看起来你已经定义了car的默认构造函数,但还没有实现它。然后你声明了一个类型为car的变量,该变量需要实现它。将Kerrek上面的代码添加到.cpp文件中就可以了:)
相关文章:
- C++:Application.cpp中抛出了未解析的外部符号(解决方案在问题的末尾,供未来的读者参考)
- 节俭并发:未解决的外部问题
- 未解决的外部链接问题
- 在 C++ 中使用 C 代码与外部"C"的问题
- 编译示例 Cinder 项目时存在大量未解决的外部问题
- 为使用boost的预开发库构建c++包装器时未解决的外部问题
- 伊利希特中未解决的外部问题
- 在Visual Studio 2013中尝试使用C++连接到mysql时未解决的外部问题
- 格鲁问题,未解决的外部问题
- 编译多源文件时未解决的外部问题
- 为什么我的程序总是得到未解决的外部问题
- TinyXml未解决的外部问题
- 使用qt模板时无法解决外部问题
- C++ 外部问题 - 未定义的引用
- c++:无法解决的外部问题让我抓狂
- MS Detours 2.1-未解决的外部问题
- 使用boost::asio(visual studio12,x64)解决的外部问题
- libcURL在7.36重建后未解决的外部问题
- 未解决的外部问题c++
- LNK 2019未解决的外部问题