issues with istream and ostream operator overloading

I've been at this for a few days now, and can't seem to find the answer, maybe someone here can help me.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void CName::WriteFullName() const
{

    cout << m_first << ' ' <<  m_last;

};

std::istream& operator>> (std::istream &inStream, CName::CName &rhs)
{
    string temp;
    cin >> temp;
    CName a(temp);
};

std::ostream& operator<< (std::ostream &outStream, const CName::CName &rhs)
{

    CName a(rhs);
    a.WriteFullName();

};


The above code is what I can't seem to get right.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
int     main(void)
{
    // test the constructors
    auto    CName       nameOne("Robert", "Bresson");
    const   CName       nameTwo = nameOne;
    auto    CName       nameThree;

    // display the contents of each newly-constructed object...

    // should see "Robert Bresson"
    cout << "nameOne = ";
    nameOne.WriteFullName();
    cout << endl;

    // should see "Robert Bresson" again
    cout << "nameTwo = ";
    nameTwo.WriteFullName();
    cout << endl;

    // should see nothing
    cout << "nameThree = ";
    nameThree.WriteFullName();
    cout << endl;

    // try the "read" function
    cout << "Enter your first and last names separated by a space: "; 
    cin >> nameThree;                                                 <--- HERE
    cout << "Your name is: " << nameThree << endl << endl;            <--- HERE
                          //the cin is not rewritting the information correctly

    // try the assignment operator
    nameOne = nameThree = nameTwo;
    cout << "Testing the assignment operator..." << endl;
    cout << "nameOne == " << nameOne << endl; 
    cout << "nameTwo == " << nameTwo << endl;
    cout << "nameThree == " << nameThree << endl << endl;


above is the main. The issue is with the lines I have pointed out in the main. I don't seem to understand how to pass the actual object so that the information is rewritten correctly in the overloaded operator.

Any help is appreciated.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// std::istream& operator>> (std::istream &inStream, CName::CName &rhs)
std::istream& operator>> ( std::istream &inStream, CName &rhs )
{
    // string temp
    string first_name, last_name ;
    
    // cin >> temp;
    if( inStream >> first_name >> last_name  )
    {
         rhs.m_first = first_name ;
         rhs.m_last = last_name ;
    }

    // CName a(temp);

    return inStream ;
} //;

//std::ostream& operator<< (std::ostream &outStream, const CName::CName &rhs)
std::ostream& operator<< ( std::ostream &outStream, const CName &rhs )
{

    // CName a(rhs);
    // a.WriteFullName();
    
    return outStream << rhs.m_first << ' ' <<  ths.m_last ;
} //; 
m_first and m_last are both pirvate char arrays to the CName class, otherwise this would work.
> m_first and m_last are both pirvate char arrays to the CName class, otherwise this would work.

So provide public accessors for them:

1
2
3
4
5
6
7
8
9
10
11
class CName
{
    // ....
    
    public: 
        std::string first_name() const { return m_first ; }
        std::string last_name() const { return m_last ; }

   // ...
    
};


And then:
1
2
3
4
5
6
7
8
9
10
11
std::istream& operator>> ( std::istream &inStream, CName &rhs )
{
    string first_name, last_name ;
    if( inStream >> first_name >> last_name  ) rhs = CName( first_name, last_name ) ;
    return inStream ;
}

std::ostream& operator<< ( std::ostream &outStream, const CName &rhs )
{
    return outStream << rhs.first_name() << ' ' <<  ths.last_name() ;
}


Prefer sd::string over char arrays; consider making m_first and m_last member variables of type std::string
Thank you for your input, what you said put me on the right track and I got the program working correctly.
Topic archived. No new replies allowed.