c++ - 编译多源文件时 Unresolved external 问题

标签 c++ unresolved-external

我有一个包含 2 个 CPP 文件(main.cpp 和 Car.cpp)和一个头文件 (Car.h) 的项目。该程序旨在允许用户输入汽车的型号、制造商和速度,并显示修改后的速度。我的问题是,当我编译项目时,我收到一个“1 unresolved externals”问题,如下所示:

1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall Car::Car(void)" (??0Car@@QAE@XZ) referenced in function _main
1>C:\Users\Shaidi\Desktop\Classes\CIST 2362\Projects\main\Debug\main.exe : fatal error LNK1120: 1 unresolved externals

这是 main.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();
}

关于c++ - 编译多源文件时 Unresolved external 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18582176/

相关文章:

c++ - 为什么类模板的成员函数声明都应该是良构的?

c++ - C++中的 namespace 搜索

c++ - 如何找出哪些方法增加了 'exe' 的大小

c++ - 什么是 undefined reference /未解析的外部符号错误以及如何修复它?

c++ - 无法找到未解析的外部符号 (c++)

c++ - 什么可能导致此链接器错误?

c++ - 派生类中的虚函数行为

c++ - 优化是否会影响使用其 PDB 调试 VC++ 应用程序的能力?

Python - Py_Initialize 在编译期间未解析

c++ - 什么是 undefined reference /未解析的外部符号错误以及如何修复它?