-1

First of all don't panic. It is a very simple program. I get this error when compiling Main.cpp with "g++ -Wall -pedantic Main.cpp" Here are all my Files. What causes the undefined reference to error?

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() {

}
XHotSniperX
  • 712
  • 7
  • 25

3 Answers3

0

You're missing the definition of the IVehicle constructor.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
0

At first glance I think IVehicle.h needs to be referenced in Car.h

#include "IVehicle.h"

Kenny Cason
  • 12,109
  • 11
  • 47
  • 72
0

This is a different problem to what you asked but you might want to note it for latter, see your code:

Car::Car(std::string name) {
    name = name;
    engine = new Engine();
}

You might want to change the parameter name so that it isn't hiding the class instance of name. Try:

Car::Car(std::string p_name) {
    name = p_name;
    engine = new Engine();
}
Anonymous
  • 21
  • 1