Data is lost somewhere ...

This is a discussion on Data is lost somewhere ... within the C++ Programming forums, part of the General Programming Boards category; Hi, i have three classes 1. Rental.H Code: /* All my includes */ .... class Driver; class Rental { public: ...

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

    Data is lost somewhere ...

    Hi, i have three classes

    1. Rental.H

    Code:
    /* All my includes */ 
    ....
    
    class Driver;
    
    class Rental
    {
    
    public:
    
    	Rental(Driver& d);
    	~Rental();
    
            ....
    
    	....
    
            void CreateNewContract();
    
    	....
    
            ....
    
    private:
    
    	Driver& driver;
    
    };
    
    #endif

    2. Driver.H

    Code:
    /* All my includes */ 
    ....
    
    
    class Driver						
    {
    public:
    	Driver();
    
           ....
    
    	void SetAddress(const Address& add);
    	Address getAddress() const;
    
           ....
    
    private:
    
           ....
    
    	Address address;
    
           ....
    };

    3. Address.H

    .....

    .....

    I then use them in Rental.cpp

    Code:
    void Rental::ReadNewAddress()
    {
           /* Declare variables & read data from use */
          
          .....
    
          .....
    
    
            /* Here' my Problem */
    	driver.getAddress().SetAddress(fline, sline, sbb, city, cde);
    }

    As you noticed, address object is a member to driver, i initialize it as you can see above ... But after this line the address object is always empty ... The function SetAddress() works well cause i used Debug to check if all its members ...

    Code:
    	std::string firstLine;
    	std::string secondLine;
    	std::string thirdLine;
    	std::string city;
    	std::string code;
    were set properly ... Is there any other way to do this except driver.getAddress().SetAddress()?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You are apparently setting the address of a temporary Address returned by Driver::GetAddress.

    There are other things I don't understand in the code. Why does Rental have a reference to Driver? This means that a Rental cannot exist without a Driver, and a Rental must refer to the same one and only Driver its entire lifetime. I would have thought that a Rental can exist without anything being rented out at the moment...

    Or why is a Rental trying to change the Address of a Driver in the first place?...
    Last edited by anon; 12-30-2008 at 09:37 AM.
    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).

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by anon View Post
    You are apparently setting the address of a temporary Address returned by Driver::GetAddress.

    There are other things I don't understand in the code. Why does Rental have a reference to Driver? This means that a Rental cannot exist without a Driver, and a Rental must refer to the same one and only Driver its entire lifetime. I would have thought that a Rental can exist without anything being rented out at the moment...
    Mhh.. true! i will try to fix that ...

    Or why is a Rental trying to change the Address of a Driver in the first place?
    On an new contract, there's a function which request data from user including the driver details which includes his/her address ... All this is done on new contract reason its done inside the Rental.cpp

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    Code:
    /* Here' my Problem */
    	driver.getAddress().SetAddress(fline, sline, sbb, city, cde);
    driver.getAddress() returns an address, but then you are trying to call SetAddress on this returned Address. You don't have a SetAddress function in the Address class, at least that you showed. And the parameters you are passing to SetAddress don't match the function declaration that you showed.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by DaveH View Post
    driver.getAddress() returns an address, but then you are trying to call SetAddress on this returned Address. You don't have a SetAddress function in the Address class, at least that you showed. And the parameters you are passing to SetAddress don't match the function declaration that you showed.
    I do ...

    Address.H

    Code:
    #include <string>
    #include <fstream>
    
    class Address						
    {
    public:
    	Address() {};
    
    	Address(const std::string& firstLine, const std::string& secondLine, const std::string& surbub, 
    		const std::string& city, const std::string& code);
    	void SetAddress(const std::string& firstLine, const std::string& secondLine, const std::string& surbub, 
    		const std::string& city, const std::string& code);
    	
    	friend std::istream& operator>>(std::istream& in, Address& address);
    	friend std::ostream& operator<<(std::ostream& out, const Address& address);
    
    	bool ReadFromFile(std::istream& indata);
    	const std::string& getFirstLine() const;
    	const std::string& getSecondLine() const;
    	const std::string& getThirdLine() const;
    	const std::string& getCity() const;
    	const std::string& getCode() const;
    private:
    	std::string firstLine;
    	std::string secondLine;
    	std::string thirdLine;
    	std::string city;
    	std::string code;
    };

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    But it seems silly that you do it this way...
    It seems more natural or better syntactically to call setAddress on Driver with a newly initialized (temporary) Address object.
    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.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    But it seems silly that you do it this way...
    It seems more natural or better syntactically to call setAddress on Driver with a newly initialized (temporary) Address object.
    Like this?

    Code:
    Address Driver::getAddress(const std::string& fline, const std::string& sline, const std::string& tline,  const std::string& cty, const std::string& cde, Address add)
    {
    	add.SetAddress(fline, sline, sbb, city, cde);
    
    	return add;
    }

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    No, I mean like:
    Code:
    driver.setAddress( Address(...) );
    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. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    No, I mean like:
    Code:
    driver.setAddress( Address(...) );
    Oh you mean through a constructor ...

    Happy New Year BTW!!!!!

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    No, I mean like:
    Code:
    driver.setAddress( Address(...) );
    A question, where should this be done? Address object is not in Rental class... And if i'm doing it inside Driver.cpp, then shouldn't this be good enough?

    Code:
    void Driver::setAddress(const std::string& fline, const std::string& sline, const std::string& tline, 
    				const std::string& cty, const std::string& cde)
    {
    	address.setAddress(fline, sline, sbb, city, cde); /* EDITED */
    }
    address being a private member of Driver class...
    Last edited by csonx_p; 01-02-2009 at 11:19 AM.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    I mean that wherever you do:
    Code:
    driver.getAddress().SetAddress(fline, sline, sbb, city, cde);
    ...you instead do...
    Code:
    driver.SetAddress( Address(fline, sline, sbb, city, cde) );
    Address is a stand-alone class that encapsulates a date.
    And GetAddress should probably return a const reference to the internal Address structure. That is typically how it is done, I think.
    It seems more natural, too.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Where's the EPIPE signal?
    By marc.andrysco in forum Networking/Device Communication
    Replies: 0
    Last Post: 12-23-2006, 07:04 PM
  3. Dynamic data members?
    By confusalot in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2005, 10:15 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. All u wanted to know about data types&more
    By SAMSAM in forum Windows Programming
    Replies: 6
    Last Post: 03-11-2003, 02:22 PM

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