My thoughts are the overloaded function should only have the four lines, not the setCarDetails()...Code:// Overload for reading input streams istream& operator >> (istream& in, Car& car) { string make, model; int ymodel, engCap; TankStatus ts; in >> make >> model; in >> ymodel; in >> engCap; in >> ts; car.setCarDetails(make, model, ymodel, engCap, ts); }
here's the setCarDetails
OR, can overloaded operators call other functions within?Code:void Car::setCarDetails(string make, string model, unsigned yrModel, unsigned engCap, TankStatus tnkStatus) { make = carMake; model = carModel; yearModel = yrModel; engineCapacity = engCap; tankStatus = tnkStatus; }
Your code is correct.
If you want to operate directly on car, you either need to make the operator>> function a friend ("friends" are trusted to not do silly things with an object, and thus can see private member variables and functions), or make it a member function of the object itself.
Whether you prefer to call a member setter function or access the member variables directly is pretty much a design decision that can be argued either way, and without any particular knowledge of the overall design/requirements, it would be impossible to say which is better. At this point, you decide what you want to do. The call of the setter has the benefit of being safer - but potentially slower because of the call overhead. I don't think performance (here at least) will be a factor, so I wouldn't worry about that bit.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Thnx mats & Elysia for your help... There's a progress...
Now i've compiled after the changes which includes the use of the '<<' & '>>' overloaded operators.. I'm gonner post them by step to avoid confusions...
read member function
it's declaration in class CarCode:void Car::read(istream& indata, Car& car, bool isKeyboard) { indata >> car; if(isKeyboard) { readDates(indata); } }
error..Code:void read(istream& i, const Car&, bool t);
?????Code:error C2511: 'void Car::read(std::istream &,Car &,bool)' : overloaded member function not found in 'Car'
Last edited by csonx_p; 07-14-2008 at 04:20 AM.
Code:// Declare class to hold car details #include <string> #include <fstream> #include <iostream> #include <iomanip> #include "RentalDate.h" using namespace std; class Car { public: Car(); enum TankStatus {unknown, empty, half, full}; void setCarDetails(string m,string ml,unsigned yrm,unsigned engCap,TankStatus ts); static string getTankStatusName(TankStatus); bool sameModel(const string&) const; bool operator<(const Car&) const; void read(istream&, const Car& c, bool); void write(ostream&, bool); string getModel(); string getMake(); unsigned getEngineCap(); unsigned getYearModel(); TankStatus getTankStatus(); private: string make; // Make as in..."toyota" string model; // Model as in ... "cressida" unsigned engineCapacity; // Engine capacity may be stored as "1800" not 1.8 unsigned yearModel; TankStatus tankStatus; // Maybe filled, half-filled, or empty }; // Overload fstream operators for enum variable ostream& operator << (ostream& out, Car::TankStatus eStatus); istream& operator >> (istream& in, Car::TankStatus& eStatus); istream& operator >> (istream& in, Car& car); void readDates(Date& dates);
Hmm. Why would you have a function that reads into a car object, that in itself is declared (and not static) in the Car class. I would either make it a free function (not part of the class), or use the current object (this) as the car object, and not pass it.
I don't know why it's going wrong - perhaps you need std::istream in your class member function?
Edit: And your problem in itself is that in your declaration in the class, you use const Car &, then loose the const in the actual function definition - you should not have const in either place in this instance, as you INTEND to modify car.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.