Question

I have a project that consists of 2 CPP files (main.cpp and Car.cpp) and a header file (Car.h). The program is meant to allow a user to enter the model, make, and speed of a car and displays the modified speed. My issue is that when I compile the project, I receive a "1 unresolved externals" issue like so:

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

Here is the main.cpp file:

// 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

Here is the Car.cpp file:

// 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;
}

And here is the Car.h file:

#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
Était-ce utile?

La solution

You miss the implementation of default constructor of Car().

class Car
{
public:
   // There is no implementation.
   Car();
}

Autres conseils

You've declared Car::Car() but never defined it. Either add a definition to the .cpp file, or remove the declaration from the header.

For example:

Car::Car()
{
}

It looks like you have defined the default constructor for car but have not implemented it. Then you declared a variable of type car which would require it to be implemented. Adding the code that Kerrek has above to the .cpp file should do the trick :)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top