Thread: Please check my C++

  1. #166
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    A static function is not passed a "this" pointer of the current class object
    Sorry about this dumb question, what is "this" pointer... i've read through it but can you put it in the context of my classes cause it's not 100% clear to me

  2. #167
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Code:
    Contract::Contract(): issued( Date::getCurrentTime() );// is this semicolon necessary?
    
    // Display the customer's contract, or save it to text file
    void Contract::Write(ostream& out, bool isScreen)
    {	
    
    }
    
    //  : error C2062: type 'void' unexpected
    //  : error C2630: ';' found in what should be a comma-separated list
    //  : error C2065: 'isScreen' : undeclared identifier
    //  : error C2065: 'out' : undeclared identifier
    //  : warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
    Nothing wrong with my write function, think error is related to code above it!

  3. #168
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You should have
    Code:
     {}
    in your Contract constructor, instead of the semicolon. Even if you don't do anything in the constructor, it does need a (empty) function body

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

  4. #169
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    Sorry about this dumb question, what is "this" pointer... i've read through it but can you put it in the context of my classes cause it's not 100% clear to me
    It is "this" object. e.g.
    Code:
    class X
    {
    public:
       X();
       void foo();
    
    private:
        int x;
    }
    
    void X::foo()
    {
       x = 7;
       // the above is short for
       this->x = 7;
    }
    Most of the time, you don't need the this pointer, but it is passed to all non-static member functions, including constructor.

    --
    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. #170
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by csonx_p View Post
    Well, by the time i initialize start time, i don't know about the end time yet. Customer can return a car beyond the requested/expected time. So i make them zeros... But i doubt if this is an issue i should be stressing about because my program won't use 'returned' date unless until the car is returned... But then yeah, i can also make start time equals returned time.. The point of this is learning C++ also. I could be writing a program which resetting to zeros is essential, by then i wouldn't like to pose this question....
    Then perhaps you should consider not having a constructor that takes no arguments instead. There's no sense in creating an "invalid" object and it saves you from design woes when an object has been initialized with a faulty time.
    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.

  6. #171
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Then perhaps you should consider not having a constructor that takes no arguments instead. There's no sense in creating an "invalid" object and it saves you from design woes when an object has been initialized with a faulty time.
    I agree with this. An object that has "nonsense" in it because the constructor placed it there is pretty meaningless.

    On a different not, more towards the contract of rentals: You may want to actually fill in the date of expected return, even if that's not the actual return date. You can then correct it when the car is actually returned.

    --
    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. #172
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    overloading '='

    Anything wrong with this overloading
    Code:
    Car::TankStatus operator=(int number, Car::TankStatus ts)
    {
    	return ts = number;
    }
    
    //: error C2801: 'operator =' must be a non-static member
    ts is enum type....

  8. #173
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I believe the left-hand side of operator= must be a user-defined type (you have int) because it can only be overloaded as a non-static member.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #174
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by anon View Post
    I believe the left-hand side of operator= must be a user-defined type (you have int) because it can only be overloaded as a non-static member.
    Lost you, LHS as in ts? that's not int, that's enum...

  10. #175
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    Lost you, LHS as in ts? that's not int, that's enum...
    If you have TWO arguments for operator=, then it must be a static (or free) function, because the class operator= would operate between "this" and the right-handside value.

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

  11. #176
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    Car::TankStatus operator=(int number, Car::TankStatus ts)
    The bold part is what would be on the left of the assignment operator.

    But anyway, it seems that you can't overload operator= for enum types anyway since it can only be overloaded as non-static member function.

    Come to think of it, how would you implement the assignment of the actual enum value, if operator= is not available (since you are in the process of defining it)?

    You can already assign enum to int and to enum, and you can assign int to enum with the help of a cast (but be careful with that since the int may not contain one of the enumerated values).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  12. #177
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Operator = must be a member of the class to which it is defined to work with.
    As thus, operator = does not work for enums, only classes.
    Either way, you should not assign an int to an enum; you're essentially circumventing the whole C++ type safety.
    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. #178
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Operator = must be a member of the class to which it is defined to work with.
    As thus, operator = does not work for enums, only classes.
    Either way, you should not assign an int to an enum; you're essentially circumventing the whole C++ type safety.
    You can certainly make "global" operator=() operator, that takes two arbitrary classes [that you have some idea of how you would convert to the left-hand side type].

    The reason, I believe, for this operator is the need to serialize the data to file. If it was stored from the enum on the file, then it should be safe to read back. We could add checks in the operator to ensure that's the case.

    --
    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. #179
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You can certainly make "global" operator=() operator, that takes two arbitrary classes [that you have some idea of how you would convert to the left-hand side type].
    But how?

    Code:
    struct X {};
    
    struct Y {};
    
    X& operator= (X& x, const Y&) //X& operator=(X&, const Y&)' must be a nonstatic member function 
    {
        return x;
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  15. #180
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Searching the web, I can't actually find it, nor in Stroustrup (but the index is a bit hard to use in Stroustrup - doesn't always list what you want).

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