Thread: Scope Related question

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    Scope Related question

    Consider my overloaded operator
    Code:
    std::istream& operator >> (std::istream& in, Contract& contract)
    {
    	std::string contractNo;
    	std::string rentalType;
    	unsigned lenderID;
    	unsigned carReg;
    
    	in >> contractNo;
    	in >> lenderID;
    	in >> carReg;
    	in >> rentalType;
    	
    	// Error on below : error C2228: left of '.SetDate' must have class/struct/union
    	Contract::issued.SetDate( Date::getCurrentTime() );
    	
    	contract.SetContractDetails(issued, carReg, rentalType,
    		lenderID, contractNo);
    
    	return in;
    }
    This operator is a free function within Contract.h file and issued is declared inside the Contract class as private

    Code:
    #include "Date.h"
    
    class Contract
    {
    public:
    	
    	Contract();
    
    private:
    
    	Date issued, returned;
    };
    
    std::istream& operator >> (std::istream& in, Contract& rental);
    How do i access SetDate() properly?
    Last edited by csonx_p; 08-23-2008 at 07:33 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Contract::issued is not an instance of a variable, it specifies the name of a variable within the Contract class, but since Contract is a type, not an object of the type Contract, it doesn't actually represent something that has any memory attached to it - it's just a type (as Elysia would say, the blueprint for a variable, not the variable itself).

    --
    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. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Note that the only time you can use "Typename::member" is if the member is static. All other accesses to member variables/functions will have to be done through an instance of the variable.

    --
    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. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    And note that since the thing is protected, you'll either need a different way of accessing it or you'll have to make operator >> a friend.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    sorry, i think i had a bit of a blonde moment there

    Code:
    contract.getIssueDate().SetDate( Date::getCurrentTime() );

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Also, I think it's not quite right to update the date in the generic output function... What if you want to print a duplicate of the contract after 3 days because the hirer is returning saying he lost it?

    Sorry, my turn to have too much bleach in the shampoo - this is the input method. Still, you probably want to READ the date stored - how else are you going to retrieve a contract that was stored 3 days ago with a correct date in it?

    --
    Mats
    Last edited by matsp; 08-23-2008 at 07:54 AM.
    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. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    how else are you going to retrieve a contract that was stored 3 days ago with a correct date in it?

    --
    Mats
    Good point

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    Also, I think it's not quite right to update the date in the generic output function... What if you want to print a duplicate of the contract after 3 days because the hirer is returning saying he lost it?

    Sorry, my turn to have too much bleach in the shampoo - this is the input method. Still, you probably want to READ the date stored - how else are you going to retrieve a contract that was stored 3 days ago with a correct date in it?

    --
    Mats
    No but hang-on... I'm setting the issued date here which is going to be stored on a file/database when i save the new contract (note the getCurrentTime function) ... I'm not viewing here!!! BTW getIssuedDate() returns the object of Date, not the actual date ... is this where the confusion is?

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Which reminds me of the mantra: "A function should do one thing, and only one thing". Along with "it should do what you expect it to do, not some other things too". Obviously, you may well find yourself writing a function that uses the contract input function, and then sets the date almost immediately after - but that is a different story. That's the "load contract and set current date" function, not the "read contract from file" function.

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

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by csonx_p View Post
    No but hang-on... I'm setting the issued date here which is going to be stored on a file/database when i save the new contract (note the getCurrentTime function) ... I'm not viewing here!!! BTW getIssuedDate() returns the object of Date, not the actual date ... is this where the confusion is?
    I think the point is that just because you're reading the data in now, doesn't mean the car wasn't rented out earlier this morning, or three days ago, or whatever. (It doesn't feel like a "real-time" sort of thing to me but a "batch mode" thing, since you're not prompting for input but reading out of presumably a text file.) So "now" is not necessarily the right time to set for the contract.

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    And even if you did read the data in for the first time right after the fact - who's to say that you won't do so again later? You might store the data in text format somewhere and read it in again, and you'll probably want to use >> for that, too.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Valid points, thanx guys.. will look at this

  13. #13
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Infact let me summarize what you saying incase i may not be getting you correctly.. Currently my File/Database is not storing the issued date which is why i am using getCurrenTime() ... So then to solve this i must now only call getCurrentTime() only when the contract function createNewContract() is called.. Then make sure the issuedDate is part of the file that i can read it anytime i need to, correct?

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Yep, that sounds right.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question related to keypad
    By lovemagix in forum C++ Programming
    Replies: 22
    Last Post: 01-05-2006, 02:25 AM
  2. A Little Performance Related SDL Question...
    By Comrade_Yeti in forum Game Programming
    Replies: 14
    Last Post: 12-09-2005, 07:46 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. keystrokes related question..
    By AmiTsur in forum C Programming
    Replies: 3
    Last Post: 11-10-2002, 09:22 AM
  5. quick question about scope resolution
    By *ClownPimp* in forum C++ Programming
    Replies: 8
    Last Post: 11-03-2002, 10:04 PM