Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

overloading operators..Please help

Status
Not open for further replies.

Tozilla

Programmer
Mar 28, 2003
3
0
0
AU
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:
#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:
#include <iostream.h>
#include <iomanip.h>
#include <string.h>
#include &quot;vehicle.h&quot;

using namespace std;

Vehicle::Vehicle()
{
strcpy(name, &quot;&quot;);
strcpy(model, &quot;&quot;);
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 <<&quot;Vehicle: &quot;<<endl; //Display the manufacturer, model, year of manufacture 
cout <<distance<<&quot;km travelled requiring &quot;<<litres<<&quot; litres of fuel at a cost of$&quot;<<cost<<endl;//total kilometers travleed, total fuel costs
if (distance=0)
{ 
cout <<&quot;No travel has been recorded yet&quot;<<endl;
cout <<&quot;No fuel has been purchased yet&quot;<<endl;
}

else
{ 
int fueleconomy = (litres/distance)*100; //fuel economy(liters per 100 kilometers)
cout << &quot;This vehicle achieved a fuel economy of &quot;<<fueleconomy<<&quot;litres/100km&quot;<<endl;
int services = distance/100;
cout << &quot;This vehicle should have undergone &quot;<<services<<&quot; service(s)&quot;<<endl; //total number of services
average = cost/litres;
cout << &quot;The average cost of fuel was $ &quot;<<average<<endl;
}
return(0);
}


this si header file for journey
PHP:
#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:
#include <iostream>
#include &quot;journey.h&quot;
using namespace std;

Journey::Journey()
{
distance = 0;
}

Journey::Journey(int distance)
{
distance = d;
}

here's the header file for FuelPurchase
PHP:
#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:
#include <iostream.h>
#include &quot;fuelpurchase.h&quot;
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:
#include <iostream>
#include <string>

using namespace std;

#include &quot;Vehicle.h&quot;
#include &quot;FuelPurchase.h&quot;
#include &quot;Journey.h&quot;

int main(int argc, char* argv[])
{
Vehicle a(&quot;BMW&quot;, &quot;A6&quot;, 2003);
Vehicle b(&quot;Toyota&quot;, &quot;A100&quot;, 2003);
Vehicle c(&quot;Mercedes-Benz&quot;, &quot;CL600&quot;, 2003);

cout << &quot;Original Statistical:&quot; << endl;
cout << &quot;=====================&quot; << 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 << &quot;Final Statistics:&quot; << endl;
cout << &quot;=================&quot; << endl;

a.print();
b.print();
c.print();

return(0);
}
 
Your post
Code:
class Vehicle
...
const operator +(const FuelPurchase& f) const;
};

that is not a valid operator+ declaration since it contains no return value. Also since an operator + should not directly manipulate the representation of an object there is no reason for it to be a member function
Code:
FuelPurchase operator+(FuelPurchase& a, FuelPurchase& b){
   FuelPurchase ret;
   // perform addition of a and b setting values of ret
   return ret;
}

-pete
 
Hi,

I have some comments for you code ..
1. Operator overloaded function like +, - etc. should return the class instance. Here, it should return vehicle object.
2. Here there is some mistake ...
Vehicle:perator +(const Journey& m); //First additional operator, &
Vehicle:perator +(const FuelPurchase& f); //Second additonal operator

(May be it is mistake while coping the code to this thread, but just make sure it is not the same in code).

I hope, this should help :)

 
Operators that should be members:
=
+=
/=
*=
* (if used as a dereference operator to a iterator class)
Notice they are all unary, and change private data members. It is common for them to return the class so you can chain assignments.

Operators that should be auxilary function in addition to the class:
+
-
*
/
Notice they are binary and should return an instance of the class.

So, for the heck of it, let's write a simple int wrapper, and impliment a +, += and the insertion operator.

Code:
class IntWraper
{
public:
   IntWraper(int a)
   {
      i = a;
   }
   IntWraper(const IntWraper& a)
   {
      i = a.i;
   }

   ~IntWraper(){} //unneeded, but good habbit to get into

    void setInt(const int& a)
    {
       i = a;
    }

    int getInt();
    {
        return i;
    }

    IntWraper operator+=(const IntWraper& a)
    {
        i = a.i + i;
        return *this;
    }

    void insert(ostream& sout)
    {
        sout << i;
    }
};

IntWraper operator+(IntWraper a,IntWraper b)
//notice non-const pass by value
{
   return a += b;
}

ostream operator<<(ostream& sout,const IntWraper& a)
{
    a.insert(sout)
    return ostream;
}
//note: if this were a memberfunction, 
//The oder would be wrong, and to call would look like:
Code:
//IntWraper i(10);
//i.<<cout;
Code:
//As it stands, the syntax will be normal as in func f()
void f()
{
    IntWraper i(4);
    IntWraper j(5);
    i += j; //i becomes 9
    cout << i;
}

This code:
Code:
Vehicle:perator +(const FuelPurchase& f); 
//Second additonal operator
{ 
totalcost = totalcost + f.cost; 
totallitrs = totallitres + f.litres; 

}
Being in the Vehicle class, this function doesn't have access to the private members of FeulPurchase. If you are really adding the liters and cost to the vehical you need to use the public inspector and mutator functions... The operator should be outside of the class and should change the passed paramaters (so pass a copy of the Vehicle to work on) and assignment should happen with an assignment operator.

Thus it looks like it should be written:
Code:
Vehicle opertator+(vehicle v,
const FuelPurchase& f)
{
   v.setTotalCost(v.getTotalCost()+f.getCost());
   v.setTotalLiters(v.getTotalLiters+f.getLiters());
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top