dimanche 21 juin 2020

can't call a function for a class member

In the following example, why I can't call car.getFuelConsumption

class Road {
public:
    double length(); // km
    int speed(); // km per hour ;
};

class Car {
protected:
    virtual double getFuelConsumption(int speed_in_km) = 0;
};

class Tank: public Car{
public:
    double getFuelConsumption(int speed_in_km) {
        return 1;
    }
};

double getPetrol(std::vector<Road> roads, const Car &car) {
    double total_fuel_consumption=0;
    for (int i=0;i<roads.size();++i)
    {
        double fuel_consumption_per_road = car.getFuelConsumption(roads[i].speed());
    }
    return total_fuel_consumption;
}

Every car has such a method so why I can't call it? For Example if I send a Tank to getPetrol() then I expect it to work and call Tank's version of getFuelConsumption()


Undefined symbols for architecture x86_64: "Road::speed()", referenced from: getPetrol(std::__1::vector<Road, std::__1::allocator >, Car const&) in main.cpp.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

Aucun commentaire:

Enregistrer un commentaire