Thread: Please check my C++

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's because you defined it as a member function for Car. It should be free and not const.
    And what's the idea of defining a local variable val, hiding the formal parameter val?
    You do not want to read into the val argument?

    Overloading something for one type may have an effect on other types if they can be implicitly converted to that type.

    Code:
    class A {};
    class B: public A {};
    
    namespace std
    {
    	ostream& operator << (ostream& rLhs, A& Status)
    	{
    		return rLhs;
    	}
    
    	istream& operator >> (istream& rLhs, A& rRhs)
    	{
    		return rLhs;
    	}
    }
    
    int main()
    {
    	A a;
    	B b;
    	cout << a;
    	cin >> a;
    	cout << b;
    	cin >> b;
    }
    Since B is derived from A, it can be implicitly converted to A, and therefore this example compiles even though there's no operator for B.
    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.

  2. #17
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    And what's the idea of defining a local variable val, hiding the formal parameter val?
    Sorry, that was a mistake...

    Alright, remember still trying to understand how classes work in C++.. So here' my second attempt

    Code:
    using namespace std;
    
    class Overloader
    {
    public :
    	ostream& operator << (ostream& rLhs, Car::TankStatus eStatus);
    	istream& operator >> (istream& rLhs, Car::TankStatus& rRhs);
    }
    
    Overloader::ostream& operator << (ostream& out, Car::TankStatus eStatus)
    {
    	out << eStatus;
    	return out;
    }
    
    Overloader::istream& operator >> (istream& in, Car::TankStatus& eStatus)
    {
    	in >> reinterpret_cast<int&>(eStatus);
    	return in;
    }
    Am i in a wrong direction?
    Last edited by csonx_p; 07-10-2008 at 02:05 AM.

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The problem is that they shouldn't be inside the class, but as free functions.
    As member functions, they can only have one argument, which is the right-hand side, but the left-hand side is ALWAYS the class itself.

    This code is illegal:
    Code:
    class Overloader
    {
    public :
    	ostream& operator << (ostream& rLhs, Car::TankStatus eStatus);
    };
    
    ostream& Overloader::operator << (ostream& rLhs, Car::TankStatus eStatus) { }
    This code is legal:
    Code:
    class Overloader
    {
    public :
    	ostream& operator << (ostream& rLhs);
    };
    
    ostream& Overloader::operator << (ostream& rLhs) { }
    And it is also the same as this code:
    Code:
    class Overloader
    {
    };
    
    ostream& Overloader::operator << (Overloader& rLhs, ostream& rRhs) { }
    No, what we're trying to write is a new type of operator << and >> which takes a Car::TankStatus and a stream to write to/from.
    That's why it needs to be free and nor part of a class.
    It does not necessarily need to be inside the std namespace either, though.
    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. #19
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Your overloaded output/input operators should NOT be in a class! If you put them in a class, they do get an additional parameter ("this", meaning the current object being operated on).

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

  5. #20
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    Your overloaded output/input operators should NOT be in a class! If you put them in a class, they do get an additional parameter ("this", meaning the current object being operated on).

    --
    Mats
    Code:
    ostream& operator << (ostream& out, TankStatus eStatus)
    {
    	out << eStatus;
    	return out;
    }
    
    istream& operator >> (istream& in, TankStatus& eStatus)
    {
    	in >> reinterpret_cast<int&>(eStatus);
    	return in;
    }
    Where do i put these two definitions? And where do i put their prototypes

  6. #21
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    Code:
    ostream& operator << (ostream& out, Car::TankStatus eStatus)
    {
    	out << eStatus;
    	return out;
    }
    
    istream& operator >> (istream& in, Car::TankStatus& eStatus)
    {
    	in >> reinterpret_cast<int&>(eStatus);
    	return in;
    }
    Where do i pout this two definitions? And where do i put their prototypes
    You can really put it wherever you like, as long as the code is compiled and linked to the application. I would probably put it together with (as in "nearby") the car input/output routines, as that is where they are used.

    The prototypes would go together with the car class declaration.

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

  7. #22
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    The prototypes would go together with the car class declaration.
    Mats
    This is what i was driving at, putting prototypes in a class (car.h) does not necessary make the function a member? This is were the confusion comes... And, if i put the definition say inside the car.cpp, seems am back to the class problem...

  8. #23
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Car.h:
    Code:
    ostream& operator << (ostream& out, Car::TankStatus eStatus);
    istream& operator >> (istream& in, Car::TankStatus& eStatus);
    Car.cpp:
    Code:
    ostream& operator << (ostream& out, Car::TankStatus eStatus)
    {
    	out << eStatus;
    	return out;
    }
    
    istream& operator >> (istream& in, Car::TankStatus& eStatus)
    {
    	in >> reinterpret_cast<int&>(eStatus);
    	return in;
    }
    Note that it simply should not be inside the Car class. The Car.h file might look like:
    Code:
    class Car
    {
    //...
    };
    
    ostream& operator << (ostream& out, Car::TankStatus eStatus);
    istream& operator >> (istream& in, Car::TankStatus& eStatus);
    In this case, the operators are not part of the class.
    However, if you do:
    Code:
    class Car
    {
    ostream& operator << (ostream& out, Car::TankStatus eStatus);
    istream& operator >> (istream& in, Car::TankStatus& eStatus);
    };
    They become part of the class, and that's not what you want.
    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.

  9. #24
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    Car.h:
    Code:
    ostream& operator << (ostream& out, Car::TankStatus eStatus);
    istream& operator >> (istream& in, Car::TankStatus& eStatus);
    Car.cpp:
    Code:
    ostream& operator << (ostream& out, Car::TankStatus eStatus)
    {
    	out << eStatus;
    	return out;
    }
    
    istream& operator >> (istream& in, Car::TankStatus& eStatus)
    {
    	in >> reinterpret_cast<int&>(eStatus);
    	return in;
    }
    Note that it simply should not be inside the Car class. The Car.h file might look like:
    Code:
    class Car
    {
    //...
    };
    
    ostream& operator << (ostream& out, Car::TankStatus eStatus);
    istream& operator >> (istream& in, Car::TankStatus& eStatus);
    In this case, the operators are not part of the class.
    However, if you do:
    Code:
    class Car
    {
    ostream& operator << (ostream& out, Car::TankStatus eStatus);
    istream& operator >> (istream& in, Car::TankStatus& eStatus);
    };
    They become part of the class, and that's not what you want.
    Oh! clear now... Will let you know

  10. #25
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Was getting a warning ...
    Code:
    warning C4717: 'operator>>' : recursive on all control paths, function will cause runtime stack overflow
    did this to solve it...

    Code:
    istream& operator >> (istream& in, Car::TankStatus& enumTStatus)
    {
    	int tStatus;
    	in >> tStatus;
    	enumTStatus = reinterpret_cast<Car::TankStatus&>(tStatus);
    	return in;
    }
    Does this look ok!

  11. #26
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    Was getting a warning ...
    Code:
    warning C4717: 'operator>>' : recursive on all control paths, function will cause runtime stack overflow
    did this to solve it...

    Code:
    istream& operator >> (istream& in, Car::TankStatus& enumTStatus)
    {
    	int tStatus;
    	in >> tStatus;
    	enumTStatus = reinterpret_cast<Car::TankStatus&>(tStatus);
    	return in;
    }
    Does this look ok!
    Yes, that looks OK to me.

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

  12. #27
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Ya, that works, but you can just do:

    Code:
    istream& operator >> (istream& in, Car::TankStatus& enumTStatus)
    {
    	in >> reinterpret_cast<int&>(tStatus);
    	return in;
    }
    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.

  13. #28
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Ya, that works, but you can just do:

    Code:
    istream& operator >> (istream& in, Car::TankStatus& enumTStatus)
    {
    	in >> reinterpret_cast<int&>(tStatus);
    	return in;
    }
    Only if enum's are the same size as int's, which not all compilers will always do - the two-variable approach is safer, and very little overhead in the whole scheme of things...

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

  14. #29
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Hmmm. Not the same size.
    Well, I guess that means a C-style cast or a temp variable.
    The latter would be preferred of course.

    :/
    Not exactly what I wanted to hear. Oh well, I bow to your superiority in this question
    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.

  15. #30
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    I bow to your superiority in this question

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, 01: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, 11: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