
March 30th, 2003, 08:11 AM
|
|
Registered User
|
|
Join Date: Sep 2002
Posts: 21
Time spent in forums: 1 h 52 m 59 sec
Reputation Power: 0
|
|
|
Debugging operator overloading
I have some errors on operator overloading. And don't know how to fix. I really need to get my program working properly by tomorrow. So please help me
here's the code:
PHP Code:
//Vehicle header file
#ifndef VEHICLE_H
#define VEHICLE_H
#define LEN 25
#include "Journey.h"
#include "Fuelpurchase.h"
class Vehicle
{
private: //variable declaration
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
Vehicle operator +(const Journey& m) const; //operator overloading
Vehicle operator +(const FuelPurchase& f) const; //operator overloading
print(); //print() method to display the result
}
#endif;
PHP Code:
//The vehicle.cpp source file
#include <iostream.h>
#include <iomanip.h>
#include <string.h>
#include "Vehicle.h"
//vehicle.cpp(13) : error C2143: syntax error : missing ';' before 'PCH creation point'
Vehicle::Vehicle()
{
strcpy(name, "");
strcpy(model, "");
year = 0;
}
Vehicle::Vehicle(const char *n, char *m, int y)
{
strncpy(name, n, LEN);
strncpy(model, m, LEN);
year = y;
}
Vehicle::Vehicle(const Vehicle &r)//copy constrcutor
{
name[LEN] = r.name[LEN];
model[LEN] = r.model[LEN];
year = r.year;
totalcost = r.totalcost;
totallitres = r.totallitres;
}
//vehicle.cpp(38) : error C2511: '+' : overloaded member function 'class Vehicle (const class Journey &)' not found in 'Vehicle'
Vehicle Vehicle::operator +(const Journey& m)
{
Vehicle v;
v.totaldistance = totaldistance + m.distance;
return (*this);
}
//vehicle.cpp(46) : error C2511: '+' : overloaded member function 'class Vehicle (const class FuelPurchase &)' not found in 'Vehicle'
Vehicle Vehicle::operator +(const FuelPurchase &f)
{
Vehicle v;
v.totalcost = totalcost + f.cost;
v.totallitres = totallitres + f.litres;
return (*this);
}
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);
}
//vehicle.h(15) : see declaration of 'Vehicle' <<<<this directs me to the beginning of the Vehicle header file
|