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:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old March 28th, 2003, 10:43 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
overloading operators..Plesae 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 : March 29th, 2003 at 01:19 AM.

Reply With Quote
  #2  
Old March 29th, 2003, 08:09 AM
pschmerg pschmerg is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: Blacksburg VA/Philly PA
Posts: 38 pschmerg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 29 m
Reputation Power: 6
Send a message via AIM to pschmerg
I think you're right the problem is withing the overloading.

I didn't look through the code too carefully, but one thing that flagged me was the declarations for the overloaded operators.

perhaps try something like:


Journey operator +(const Journey& m) const;

and then change the appropriate type for the other declaration. I'm not exactly sure what type incrimenting FuelPurchase will do, but try changing the return types in the declaration and implementation.

Reply With Quote
  #3  
Old March 30th, 2003, 03:26 AM
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
Here are two problems:

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

What are the return types of those two functions?

Also, when you post an error message and several hundred lines of code, it would be very helpful if you posted the line the error occurred on.

Last edited by 7stud : March 30th, 2003 at 03:33 AM.

Reply With Quote
  #4  
Old March 30th, 2003, 03:30 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
Quote:
Originally posted by 7stud
Here are two problems:

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

What are the return types of those two functions?

Also, when you post an error message and a several hundred lines of code, it would be very helpful if you posted the line the error occurred on.


you mean the error message?

I only got three error messages:

vehicle.cpp(13) : error C2143: syntax error : missing ';' before 'PCH creation point'
vehicle.cpp(38) : error C2511: '+' : overloaded member function 'class Vehicle (const class Journey &)' not found in 'Vehicle'
vehicle.h(15) : see declaration of 'Vehicle'
vehicle.cpp(46) : error C2511: '+' : overloaded member function 'class Vehicle (const class FuelPurchase &)' not found in 'Vehicle'
vehicle.h(15) : see declaration of 'Vehicle'

Reply With Quote
  #5  
Old March 30th, 2003, 03:36 AM
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
Most people, including me, are not going to count lines to find out where the error is. If you can't take the time to comment your code to point to the lines where the errors are, I don't think you should expect people to take the time to help you.

Reply With Quote
  #6  
Old March 30th, 2003, 03:39 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
Quote:
Originally posted by 7stud
Here are two problems:

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

What are the return types of those two functions?

Also, when you post an error message and a several hundred lines of code, it would be very helpful if you posted the line the error occurred on.


you mean the error message?

I only got four error messages:
PHP Code:
//vehicle.cpp(13) :  error C2143: syntax error : missing ';' before 'PCH creation point'
Vehicle::Vehicle()    
{
  
strcpy(name"");
  
strcpy(model"");
  
year 0;



PHP Code:
 Vehicle::Vehicle(const char *nchar *mint y)    
{
  
strncpy(namenLEN);
  
strncpy(modelmLEN);
  
year y;



PHP Code:
 Vehicle::Vehicle(const Vehicle &r)  {
 
name[LEN] = r.name[LEN];
 
model[LEN] = r.model[LEN];
 
year r.year;
 
totalcost r.totalcost;
 
totallitres r.totallitres;
 } 



PHP Code:
//vehicle.cpp(38) : error C2511: '+' : overloaded member function 'class Vehicle (const class Journey &)' not found in 'Vehicle'

Vehicle Vehicle::operator +(const Journeym)    //First additional operator
{    
    
Vehicle v;
    
v.totaldistance totaldistance m.distance;
    return (*
this);




PHP Code:
//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); 




PHP Code:
//vehicle.h(15) : see declaration of 'Vehicle' <<<<this directs me to the beginning of the Vehicle header file 

Last edited by Tozilla : March 30th, 2003 at 03:50 AM.

Reply With Quote
  #7  
Old March 30th, 2003, 04:39 AM
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
Now I refer you to the first four lines of my initial post.

Reply With Quote
  #8  
Old March 30th, 2003, 04:53 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
Quote:
Originally posted by 7stud
Now I refer you to the first four lines of my initial post.


It should be like this for returning values?

PHP Code:
//overloading operator in Vehicle header file
Vehicle operator +(const Journeym) const; 
Vehicle operator +(const FuelPurchasef) const; 


PHP Code:
//implmentation in Vehicle source file
Vehicle Vehicle::operator +(const Journeym)    
{    
    
Vehicle v;
    
v.totaldistance totaldistance m.distance;
    return (*
this);

}            

Vehicle Vehicle::operator +(const FuelPurchase &f)
{
    
Vehicle v;
    
v.totalcost totalcost f.cost
    
v.totallitres totallitres f.litres
    return (*
this); 


Reply With Quote
  #9  
Old March 30th, 2003, 03:31 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's the same

Reply With Quote