-1

首先不要惊慌。这是一个非常简单的程序。使用“g++ -Wall -pedantic Main.cpp”编译 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() {

}

汽车.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();

};

汽车.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;
}

宝马.h

class BMW : public Car {
    private: 
        BMWLogo* bmwLogo;
    public:
        BMW();
};

宝马.cpp

#include "BMW.h"

BMW::BMW()
: Car("BMW") {
    bmwLogo = new BMWLogo();
}

引擎.h

class Engine {

    Engine();

};

引擎.cpp

#include "Engine.h"

Engine::Engine() {

}

宝马Logo.h

class BMWLogo {

    BMWLogo();

};

宝马标志.cpp

#include "BMWLogo.h"

BMLogo::BMWLogo() {

}
4

3 回答 3

0

您缺少IVehicle构造函数的定义。

于 2013-05-06T22:42:15.853 回答
0

乍一看,我认为 IVehicle.h 需要在 Car.h 中引用

#include "IVehicle.h"

于 2013-05-06T22:46:15.637 回答
0

这与您所问的问题不同,但您可能想为后者注意它,请参阅您的代码:

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

您可能想要更改参数名称,以便它不会隐藏名称的类实例。尝试:

Car::Car(std::string p_name) {
    name = p_name;
    engine = new Engine();
}
于 2013-05-06T22:47:46.773 回答