I have many errors when I compiled it. I think it's all from overloading operator. I have so many errors...this is one of them
binary '+' : no operator defined which takes a right-hand operand of type 'class FuelPurchase' (or there is no acceptable conversion. Can anyone fix please this for me?
Here's my coding
Header file of vehicle
PHP Code:
#ifndef VEHICLE_H
#define VEHICLE_H
using namespace std;
static const int LEN = 25;
class Vehicle
{
private:
char name[LEN];
char model[LEN];
int year;
int totalcost;
int totallitres;
public:
Vehicle();
Vehicle(const char *n, char *m, int y);
Vehicle(const Vehicle &r); //copy constructor
const operator +(const Journey& m) const;
const operator +(const FuelPurchase& f) const;
print();
};
#endif
source file of Vehicle
PHP Code:
#include <iostream.h>
#include <iomanip.h>
#include <string.h>
#include "vehicle.h"
using namespace std;
Vehicle::Vehicle()
{
strcpy(name, "");
strcpy(model, "");
year = 0;
}
Vehicle::Vehicle(char *n, char *m, int y)
{
strncpy(name, n, LEN);
strncpy(model, m, LEN);
year = y;
}
Vehicle::Vehicle(const Vehicle &r) //copy constrcutor
{
name = r.name;
model = r.model;
year = r.year;
totalcost = r.totalcost;
totallitres = r.totallitres;
}
Vehicle:perator +(const Journey& m); //First additional operator
{
totaldistance = totaldistance + m.distance;
}
Vehicle:perator +(const FuelPurchase& f); //Second additonal operator
{
totalcost = totalcost + f.cost;
totallitrs = totallitres + f.litres;
}
Vehicle:Print()
{ int distance=0,services=0,cost=0,litres=0; //all new vehicles begins with zero kilometers travelled and zero fuel
double fueleconomy=0, average=0; //Set number of decimal places
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(1);
cout <<"Vehicle: "<<endl; //Display the manufacturer, model, year of manufacture
cout <<distance<<"km travelled requiring "<<litres<<" litres of fuel at a cost of$"<<cost<<endl;//total kilometers travleed, total fuel costs
if (distance=0)
{
cout <<"No travel has been recorded yet"<<endl;
cout <<"No fuel has been purchased yet"<<endl;
}
else
{
int fueleconomy = (litres/distance)*100; //fuel economy(liters per 100 kilometers)
cout << "This vehicle achieved a fuel economy of "<<fueleconomy<<"litres/100km"<<endl;
int services = distance/100;
cout << "This vehicle should have undergone "<<services<<" service(s)"<<endl; //total number of services
average = cost/litres;
cout << "The average cost of fuel was $ "<<average<<endl;
}
return(0);
}
this si header file for journey
PHP Code:
#ifndef JOURNEY_H
#define JOURNEY_H
class Journey
{
private:
int distance;
public:
Journey();
Journey(const int distance);
};
#endif
this is source file file journey
PHP Code:
#include <iostream>
#include "journey.h"
using namespace std;
Journey::Journey()
{
distance = 0;
}
Journey::Journey(int distance)
{
distance = d;
}
here's the header file for FuelPurchase
PHP Code:
#ifndef FUELPURCHASE_H
#define FUELPURCHASE_H
class FuelPurchase
{
private:
int cost;
int litres;
public:
FuelPurchase();
FuelPurchase(const int c, int l);
};
#endif
This is source file for FuelPurchase
PHP Code:
#include <iostream.h>
#include "fuelpurchase.h"
using namespace std;
FuelPurchase::FuelPurchase()
{
cost = 0;
litres = 0;
}
FuelPurchase::FuelPurchase(int c, int l);
{
cost = c;
litres = l;
}
Here's the main code...
PHP Code:
#include <iostream>
#include <string>
using namespace std;
#include "Vehicle.h"
#include "FuelPurchase.h"
#include "Journey.h"
int main(int argc, char* argv[])
{
Vehicle a("BMW", "A6", 2003);
Vehicle b("Toyota", "A100", 2003);
Vehicle c("Mercedes-Benz", "CL600", 2003);
cout << "Original Statistical:" << 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);
}