Please check my C++

This is a discussion on Please check my C++ within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by Elysia Yeah, that looks like a date class, but sometimes you might not want to specify everything, ...

  1. #76
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    Yeah, that looks like a date class, but sometimes you might not want to specify everything, such as perhaps minutes and seconds.
    You could make them optional
    While we on date ..

    Code:
    void Menu::setReturnDates(const Date& dates)
    {
    	int day, month, year;
    
    	cout << "\nEnter return dates [DAY - MONTH - YEAR]" << endl;
    	cin >> day;
    	cin >> month; 
    	cin >> year;
    	
    	dates.setTimeDate(day, month, year, 0, 0, 0);
    }
    Q.

    1. Should i make Date const, got an error
    Code:
     error C2662: 'Date::setTimeDate' : cannot convert 'this' pointer from 'const Date' to 'Date &'
    2. sometimes when i type dates + '.' the members list does not come up (or drop down), should i be worried? I mean is date class visible even in that case? setReturnDates is a member of Menu.

  2. #77
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You should never have const reference when you intend to call setters.

    As Elysia suggested, perhaps you'd like to use default values for the h, m and s parameters so that you don't have to give a bunch of zero's.

    --
    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.

  3. #78
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Quote Originally Posted by csonx_p View Post
    1. Should i make Date const, got an error
    Code:
     error C2662: 'Date::setTimeDate' : cannot convert 'this' pointer from 'const Date' to 'Date &'
    Because setTimeDate is not const and you can only call const functions from an object that is const. Since a setter can never be const, you should not take the argument by const reference.

    2. sometimes when i type dates + '.' the members list does not come up (or drop down), should i be worried? I mean is date class visible even in that case? setReturnDates is a member of Menu.
    Sometimes it screws up. And sometimes it screws up due to an error in your code.
    If it happens, try compiling the current code and fixing the mistakes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #79
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    As Elysia suggested, perhaps you'd like to use default values for the h, m and s parameters so that you don't have to give a bunch of zero's.
    Mats
    By this you referring to getting the current time from system maybe? Or just set to any hour, minute... I removed the seconds, no relevancy to this program really

  5. #80
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    By this you referring to getting the current time from system maybe? Or just set to any hour, minute... I removed the seconds, no relevancy to this program really
    I mean you set hour and minute (and second?) to zero if they are not specified.

    --
    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.

  6. #81
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    overloading

    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);
    }
    My thoughts are the overloaded function should only have the four lines, not the setCarDetails()...

    here's the setCarDetails
    Code:
    void Car::setCarDetails(string make, string model, unsigned yrModel, 
    						unsigned engCap, TankStatus tnkStatus)
    {
    	make = carMake;
    	model = carModel;
    	yearModel = yrModel;
    	engineCapacity = engCap;
    	tankStatus = tnkStatus;
    }
    OR, can overloaded operators call other functions within?

  7. #82
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    There's no restrictions to what operators can call.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #83
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  9. #84
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    flood of errors

    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

    Code:
    void Car::read(istream& indata, Car& car, bool isKeyboard)
    {
    	indata >> car;
    	if(isKeyboard) {
    		readDates(indata);
    	}
    }
    it's declaration in class Car

    Code:
    void read(istream& i, const Car&, bool t);
    error..
    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.

  10. #85
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    You forgot to change your class definition to match your new implementation.
    You made a mistake somewhere.
    And a tip for you: name your prototypes exactly as your implementation.
    Ie:

    void Car::read(istream& indata, Car& car, bool isKeyboard)
    And
    void read(istream& indata, Car& car, bool isKeyboard);

    Makes it more readable.
    (As you will see, Visual Studio intellisense gets its information from your definition.)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #86
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    Car Class

    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);

  12. #87
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    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

    Code:
    void Car::read(istream& indata, Car& car, bool isKeyboard)
    {
    	indata >> car;
    	if(isKeyboard) {
    		readDates(indata);
    	}
    }
    it's declaration in class Car

    Code:
    void read(istream& i, const Car&, bool t);
    error..
    Code:
    error C2511: 'void Car::read(std::istream &,Car &,bool)' : overloaded member function not found in 'Car'
    ?????
    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.

  13. #88
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    error C2511: 'void Car::read(std::istream &,Car &,bool)' : overloaded member function not found in 'Car'
    You sure that the ONLY error you get?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #89
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    You sure that the ONLY error you get?
    NOOOO! that was the first, but now got different ones after modifications...

    First, modifications

    Code:
    void Car::read(istream& indata, Car& car, bool isKeyboard) // removed const before car
    {
    	Date dates; // added
    
    	indata >> car;
    	if(isKeyboard) {
    		readDates(dates); // changed argument from stream to dates
    	}
    }
    New errors...

    1.
    Code:
    // Overload for enum variable when writing data
    ostream& operator << (ostream& out, Car::TankStatus enumTStatus)
    {
    	out << reinterpret_cast<int&>(enumTStatus);
    	return out;
    }
    
    // Error 1
    : error C2084: function 'std::ostream &operator <<(std::ostream &,Car::TankStatus)' already has a body
    2.

    Code:
    // Ouput the data to screen or file
    void Car::write(ostream& out, bool isScreen, Date& date)
    {
    	if(isScreen)	// To the output screen
    	{
    		out << setw(6) << make << setw(8) << model;
    		out << setw(10) << yearModel;
    		out << setw(12) << engineCapacity;
    		out << setw(9) << date.day << setw(3) << date.month << setw (5) << date.year;
    		out << setw(13) << getTankStatusName(tankStatus).c_str();
    		puts("");
    	}
    	else			// To the file
    	{
    		out << " " << make << " " <<  model;
    		out << " " << yearModel;
    		out << " " << engineCapacity;
    		out << " " << returnDate.day << " " << returnDate.month << " " << returnDate.year;
    		out << " " << tankStatus;
    	}
    }
    
    // Error 2
    : error C2511: 'void Car::write(std::ostream &,bool,Date &)' : overloaded member function not found in 'Car'
    i'll pause here...

    EDIT: Picked up some of my probs in red, and, am not using getters where am suppose to
    Last edited by csonx_p; 07-14-2008 at 04:51 AM.

  15. #90
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    the function write has no date in the Car declaration, it has in your write function defintion.

    Code:
    	out << reinterpret_cast<int&>(enumTStatus);
    Are you sure that you want to cast it to int&?

    Are you sure the problem with operator>> on tankstatus isn't that you are declaring two outputs instead of one input and one output?

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. how to check input is decimal or not?
    By kalamram in forum C Programming
    Replies: 3
    Last Post: 08-31-2007, 07:07 PM
  3. Please check this loop
    By Daesom in forum C++ Programming
    Replies: 13
    Last Post: 11-02-2006, 12:52 AM
  4. A way to check for Win98 or WinXP
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 10-31-2002, 10:06 AM
  5. how to check for end of line in a text file
    By anooj123 in forum C++ Programming
    Replies: 6
    Last Post: 10-24-2002, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21