Thread: overloading operators..please help

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    13

    overloading operators..please help

    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 *nchar *mint y);
    Vehicle(const Vehicle &r); //copy constructor
    const operator +(const Journeym) const;
    const 
    operator +(const FuelPurchasef) 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 *nchar *mint y
    {
    strncpy(namenLEN);
    strncpy(modelmLEN);
    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 Journeym); //First additional operator
    {
    totaldistance totaldistance m.distance;



    Vehicle:perator +(const FuelPurchasef); //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=0average=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 cint 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 cint 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 argccharargv[])
    {
    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();

    FuelPurchase(5060);
    Journey(150);
    FuelPurchase(1215);

    FuelPurchase(5060);
    Journey(250);
    FuelPurchase(2222);

    FuelPurchase(5060);
    Journey(350);
    FuelPurchase(5040);

    cout << "Final Statistics:" << endl;
    cout << "=================" << endl;

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

    return(
    0);

    Last edited by Tozilla; 03-29-2003 at 02:01 AM.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    code tags and disabled smilies and i might take a look at it.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    its 4.50am, im stoned,tired and eyes are blurry but i can see several mistakes. Read the thread on addition operator recently.
    Notice how you have misprototyped in you vehicle.h
    Make sure funcs match prototypes.
    You have several classes that hold private data but you offer no means at getting at it. No friends or accessors spells trouble i think especially when you want to write those operators.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    13
    Originally posted by Stoned_Coder
    its 4.50am, im stoned,tired and eyes are blurry but i can see several mistakes. Read the thread on addition operator recently.
    Notice how you have misprototyped in you vehicle.h
    Make sure funcs match prototypes.
    You have several classes that hold private data but you offer no means at getting at it. No friends or accessors spells trouble i think especially when you want to write those operators.
    hm...I still can't make my program working properly. I fixed the prototypes. I still can see the same errors.
    Last edited by Tozilla; 03-29-2003 at 04:29 AM.

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You have declared a + operator function, but you never define it anywhere. Also, it needs a proper return value, not just const.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    13
    i've aleady fixed that problem. But still have errors.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about overloading operators
    By *DEAD* in forum C++ Programming
    Replies: 9
    Last Post: 05-08-2008, 10:27 AM
  2. Replies: 16
    Last Post: 10-27-2007, 12:42 PM
  3. Overloading fstream's << and >> operators
    By VirtualAce in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2007, 03:17 AM
  4. operators overloading
    By waqasriazpk in forum C++ Programming
    Replies: 1
    Last Post: 07-26-2002, 01:05 AM
  5. Overloading operators...
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 11-21-2001, 08:24 PM