I wrote a program and header files that compiles and runs correctly (Visual C++), but when I try to write implementation files for the headers the program no longer works, it compiles but when I try to run it I get 6 unresolved externals errors eg
main.obj : error LNK2001: unresolved external symbol "public: class Vehicle __thiscall Vehicle:perator+(class Journey &)" (??HVehicle@@QAE?AV0@AAVJourney@@@Z)
Debug/main.exe : fatal error LNK1120: 6 unresolved externals
, I am not sure what I am doing wrong as I am new to this.
This is the code as it works
Code:
=========Header file 1
#include <iostream>
using namespace std;
class FuelPurchase;
class Journey;
class Vehicle
{
public:
Vehicle(string makeValue, string modelValue, int yearValue);
Vehicle (int yearValue);
Vehicle();
void Print();
Vehicle b(Vehicle);
int vCost;
int vLitre;
int vKm;
Vehicle operator +(FuelPurchase &total);
Vehicle operator +(Journey &km);
private:
int year;
string make;
string model;
};
=========Header file 2
#include <iostream>
using namespace std;
class Journey
{
public:
Journey(float journeyValue);
Journey();
float journey;
};
=========Header file 3
#include <iostream>
using namespace std;
class FuelPurchase
{
public:
FuelPurchase(int litreValue, int costValue);
int cost;
int litre;
};
=============main.cpp
#include <iostream>
#include <string>
#include <iomanip>
#include "Vehicle.h"
#include "FuelPurchase.h"
#include "Journey.h"
using namespace std;
int main(int argc, char* argv[])
{
Vehicle a("Audi", "A6", 2003);
Vehicle b("Jaguar","X200", 2003);
Vehicle c("Mercedes-Benz","CL600",2003);
cout<< "Original statistics:" << endl;
cout<< "====================" << endl;
a.Print();
b.Print();
c.Print();
a = a + FuelPurchase (50, 60);
a = a + Journey (150);
a = a + FuelPurchase (12, 15);
b = b + FuelPurchase (50, 60);
b = b + Journey (250);
b = b + FuelPurchase (22, 22);
c = c + FuelPurchase (50, 60);
c = c + Journey (350);
c = c + FuelPurchase (50, 40);
cout<< "Final statistics:" << endl;
cout<< "=================" << endl;
a.Print();
b.Print();
c.Print();
return(0);
}
Vehicle::Vehicle(string make_, string model_, int year_)
: make(make_), model(model_), year(year_)
{
vCost = 0;
vLitre = 0;
vKm = 0;
}
Vehicle::Vehicle(int year_)
: year(year_)
{
}
FuelPurchase::FuelPurchase(int cost_, int litre_)
: cost(cost_), litre(litre_)
{
}
Journey::Journey(float journey_)
: journey(journey_)
{
}
Vehicle Vehicle::operator+(FuelPurchase &total)
{
Vehicle Update( *this);
Update.vLitre += total.litre;
Update.vCost += total.cost;
return Update;
}
Vehicle Vehicle::operator+(Journey &km)
{
Vehicle Update( *this);
Update.vKm += km.journey;
return Update;
}
void Vehicle::Print()
{
cout<<"Vehicle: "<<make<<" "<<model<< endl;
cout<<vKm<<" travelled requiring "<<vLitre<<" of fuel at a cost of $"<<vCost<<endl;
// and so on
}
What I did next was to change the relevant procedures into an implementation file, eg:
Code:
=============vehicle.cpp
#include "Vehicle.h"
#include <iostream>
#include <string>
#include <iomanip>
#include "FuelPurchase.h"
#include "Journey.h"
using namespace std;
Vehicle::Vehicle()
{
vCost = 0;
vLitre = 0;
vKm = 0;
model = "";
make = "";
vKm = 0;
}
Vehicle::Vehicle(string make_, string model_, int year_)
: make(make_), model(model_), year(year_)
{
}
Vehicle Vehicle::operator+(FuelPurchase &total)
{
Vehicle Update( *this);
Update.vLitre += total.litre;
Update.vCost += total.cost;
return Update;
}
Vehicle Vehicle::operator+(Journey &km)
{
Vehicle Update( *this);
Update.vKm += km;
return Update;
}
void Vehicle::Print()
{
// same as working program
}
=============Journey.cpp
#include "Journey.h"
#include <iostream>
using namespace std;
Journey::Journey()
{
km= 0;
}
Journey::Journey(int km_)
: km(km_)
{
}
=============FuelPurchase.cpp
#include <iostream>
using namespace std;
#include "FuelPurchase.h"
FuelPurchase::FuelPurchase()
{
}
FuelPurchase::FuelPurchase(int cost_, int litre_)
: cost(cost_), litre(litre_)
{
cost = 0;
litre =0;
}
The main code ends now with the return value, but is otherwise the same.
If anyone can help me, it would be much appreciated!