#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2002
    Posts
    21
    Rep 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 *nchar *mint y);
            
    Vehicle(const Vehicle &r);                          //copy constructor
            
    Vehicle operator +(const Journeym) const;            //operator overloading
            
    Vehicle operator +(const FuelPurchasef) 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 *nchar *mint y)    
    {
      
    strncpy(namenLEN);
      
    strncpy(modelmLEN);
      
    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 Journeym)    
    {    
        
    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=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);
    }

    //vehicle.h(15) : see declaration of 'Vehicle' <<<<this directs me to the beginning of the Vehicle header file 
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    Your function headers for your function definitions are:

    Vehicle Vehicle::operator +(const Journey& m)
    Vehicle Vehicle::operator +(const FuelPurchase &f)

    and your function declarations are:

    Vehicle operator +(const Journey& m) const;
    Vehicle operator +(const FuelPurchase& f) const;

    Are those the same?
    Last edited by 7stud; March 30th, 2003 at 02:59 PM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2002
    Posts
    21
    Rep Power
    0
    I just mistyped when I pasted here. It shoud be like this
    Code:
    //headers for  function definitions 
    Vehicle Vehicle::operator +(const Journey& m)    
    Vehicle Vehicle::operator +(const FuelPurchase& f)
    Code:
    //function declarations 
    Vehicle operator +(const Journey& m) const;  
    Vehicle operator +(const FuelPurchase& f) const;
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    And once again I pose the question to you: are the function headers and function declarations the same?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2002
    Posts
    21
    Rep Power
    0
    yes..it is the same
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    Clearly, that is not the case since there are a different number of characters making up the declarations and the headers. Post the characters that are different in the declarations and the function headers, and I think you will see they are different.
    Last edited by 7stud; March 30th, 2003 at 06:10 PM.

IMN logo majestic logo threadwatch logo seochat tools logo