C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
1200+ fellow developers rate and compare features of the top IDEs, like Visual Studio, Eclipse, RAD, Delphi and others, across 13 categories. Enjoy this FREE Download of the IDE User Satisfaction Study by Evans Data Corporation. Download Now!
  #1  
Old March 30th, 2003, 08:11 AM
Tozilla Tozilla is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2002
Posts: 21 Tozilla User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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 *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 

Reply With Quote
  #2  
Old March 30th, 2003, 01:57 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
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 01:59 PM.

Reply With Quote
  #3  
Old March 30th, 2003, 02:08 PM
Tozilla Tozilla is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2002
Posts: 21 Tozilla User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 52 m 59 sec
Reputation 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; 

Reply With Quote
  #4  
Old March 30th, 2003, 03:28 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
And once again I pose the question to you: are the function headers and function declarations the same?

Reply With Quote
  #5  
Old March 30th, 2003, 03:33 PM
Tozilla Tozilla is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2002
Posts: 21 Tozilla User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 52 m 59 sec
Reputation Power: 0
yes..it is the same

Reply With Quote
  #6  
Old March 30th, 2003, 03:37 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
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 05:10 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Debugging operator overloading


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway