After reading past posts about undefined references to vtables, I'm still unsure of why I'm getting an error with mine. I thought I used the proper overrides but I have been unable to get this error to go away. I thought my initial issue might be with overriding or the destructor but the changes I attempted did not seem to fix anything. I'm also getting another undefined reference to one of my functions within my original class. Any advice here would be greatly appreciated.
test.cpp
#include <iostream>
#include <vector>
#include <cstdlib>
#include <algorithm>
#include "CarFactory.hpp"
#include <ctime>
#include <numeric>
using namespace std;
// same function with an STL algorithm
CarFactory *getLeastBusyFactory(const vector<CarFactory*> &inFactories) {
if (inFactories.size() == 0) return nullptr;
return accumulate(inFactories.begin()+1, inFactories.end(), *inFactories.begin(),
[](CarFactory *a, CarFactory *b)
{ return a->getNumCarsInProduction() < b->getNumCarsInProduction() ? a: b; }
);
}
int main() {
vector<CarFactory*> factories;
srand(time(nullptr));
// Create 3 Ford factories and
// 1 Toyota factory with random load.
factories.push_back(new FordFactory("Chicago, IL", rand()%4));
factories.push_back(new FordFactory("Dearborn, MI", rand()%4));
factories.push_back(new FordFactory("Louisville, KY", rand()%4));
factories.push_back(new ToyotaFactory("Lafayette,IN", rand()%4));
for (size_t i=0; i < 10; ++i) {
CarFactory *currentFactory = getLeastBusyFactory(factories);
Car *newCar = currentFactory->requestCar();
cout << "Ordering " << newCar->getMake() << " " << newCar->getModel()
<< " from " << currentFactory->getLocation()
<< endl;
}
}
CarFactory.hpp //This is called from the test.cpp
#include <string>
// abstract product
class Car{
public:
std::string getMake() const {return make_;}
std::string getModel() const {return model_;}
protected:
std::string make_; // this car's make
std::string model_; // this car's model
};
// two concrete products
class Ford : public Car{
public:
Ford();
};
class Toyota : public Car{
public:
Toyota();
};
// absract factory
class CarFactory {
public:
CarFactory(std::string location, int numCarsInProduction):
location_(location),
numCarsInProduction_(numCarsInProduction){}
Car* requestCar();
int getNumCarsInProduction() const {return numCarsInProduction_;}
std::string getLocation() const {return location_;}
virtual ~CarFactory(){}
protected:
virtual Car* makeCar() =0;
private:
int numCarsInProduction_;
std::string location_;
};
// two concrete factories
class FordFactory : public CarFactory {
public:
FordFactory(std::string location="", int numCarsInProduction=0):
CarFactory(location, numCarsInProduction) {}
protected:
Car* makeCar() override;
};
class ToyotaFactory : public CarFactory {
public:
ToyotaFactory (std::string location="",
int numCarsInProduction=0):
CarFactory(location, numCarsInProduction){}
protected:
Car* makeCar() override;
};
Here are the errors I am getting:
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\Jaden\AppData\Local\Temp\ccWdlJfk.o:CarTest.cpp:(.text+0x370): undefined reference to `CarFactory::requestCar()'
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\Jaden\AppData\Local\Temp\ccWdlJfk.o:CarTest.cpp:(.rdata$.refptr._ZTV13ToyotaFactory[.refptr._ZTV13ToyotaFactory]+0x0):
undefined reference to `vtable for ToyotaFactory'
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\Jaden\AppData\Local\Temp\ccWdlJfk.o:CarTest.cpp:(.rdata$.refptr._ZTV11FordFactory[.refptr._ZTV11FordFactory]+0x0): undefined reference to `vtable for FordFactory'
collect2.exe: error: ld returned 1 exit status
Aucun commentaire:
Enregistrer un commentaire