2013-09-02 4 views
0

У меня есть проект, состоящий из 2 файлов CPP (main.cpp и Car.cpp) и файла заголовка (Car.h). Программа предназначена для того, чтобы пользователь мог ввести модель, сделать и скорость автомобиля и отобразить измененную скорость. Моя проблема заключается в том, что при компиляции проекта, я получаю «1 неразрешенных внешних» вопрос так:Неразрешенная внешняя проблема при компиляции файлов с несколькими источниками

1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall Car::Car(void)" ([email protected]@[email protected]) 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 

ответ

2

Вы пропустите реализацию конструктора Car() по умолчанию.

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

Вы объявили Car::Car(), но не определили его. Либо добавьте определение в файл .cpp, либо удалите объявление из заголовка.

Например:

Car::Car() 
{ 
} 
0

Похоже, что вы определили конструктор по умолчанию для автомобиля, но не внедрили его. Затем вы объявили переменную типа автомобиля, которая потребовала бы ее реализации. Добавление кода, который Kerrek имеет к файлу .cpp, должен делать трюк :)

Смежные вопросы