How do I do that?

This is a discussion on How do I do that? within the C++ Programming forums, part of the General Programming Boards category; I wrote a class called MyString. It uses NTS string, c-style. Basically consists of constructors, copy constructor and destructor. Nothing ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    99

    Thumbs down How do I do that?

    I wrote a class called MyString. It uses NTS string, c-style. Basically consists of constructors, copy constructor and destructor. Nothing else.
    I also wrote another class, called People.
    Code:
    class People {
    private:
       MyString    name;
       MyString    address;
       int         age;
    public:
       // methods such as constructors asking user to provide
       // all the info to instantiate an object. Did not have a problem
       // with assigning values to MyString name or MyString address.
    I need to write a show() method in this class which will display all its private members...... Here I encountered problems. First it did not compile , it complained that << does not defined in there. OK, so I tried to write a method overloading the operator << to display MyString types data members.
    Here it is:
    Code:
    ostream& operator<<(ostream& os, const MyString &s) {
                 return os<<s.getName()<<" "<<s.getAddress()<<endl;
                 }
    The compiler complains that
    `std::ostream& People::operator<<(std::ostream&, const MyString&)' must take exactly one argument
    and then that
    'const class MyString' has no member named 'getName'
    -- well, it doesn't because it is a public method for People class, not MyString class......
    Can anybody help me solve this puzzle? Where am I wrong?

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    >> OK, so I tried to write a method overloading the operator << to display MyString types data members.

    well it looks like you're mainipulating a People object within the function.

    >> The compiler complains that

    make it a friend function.

    Code:
     struct People
     {
    	friend 
    	ostream & 
    	operator << (ostream & os, const People & me)
    	{
    		return os<<me.getName()<<" "<<me.getAddress()<<endl;
    	}	
     };
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    99
    Maybe I am doing that wrong, but now the compiler lists << as no match again...
    What I was thinking, maybe it has a problem with the return type of getName() and getAddress()...... It is MyString.
    Code:
    MyString getAddress() const { return address; }
    Would that cause the problem?

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    did you overload MyString with operator << as well?
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    99
    no. Should I?

    -----------
    Well, I tried that too. It compiles fine, but when I run the program it displays a blank line where the MyString vars (from People class) should go.
    I tried to do this:
    Code:
    friend ostream& operator<<(ostream& os, const MyString &s) {
                 return os<<s.str<<" "<<endl;
                 }  
        void showit() { cout << str; }
    -- that's in MyString class, and it works.......... Why it doesn't work for People class?
    Last edited by kocika73; 04-07-2006 at 10:19 PM. Reason: tried that

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Does your copy constructor for the MyString class work correctly?

    Also, your MyString class operator<< shouldn't havethe space and the newline from a design perspective. Just have it output the string and the People class operator<< can do the formatting instead.

Popular pages Recent additions subscribe to a feed

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