Keword "friend" unneeded required.

2004-09-21
2012-09-26
  • okay, just wow. you totally misunderstood what i said. read it again. you also seem to misunderstand the object of headers in general.

    extern is depcriciated because all functions are extern by default.

    obag

     
  • SoaringFalcon
    SoaringFalcon
    2004-09-21

    If this was your answer (lot of "nobodies" in here" )...

    > put

    > ostream& operator<<(ostream& os, const Person& p);

    > and

    > istream& operator>>(istream& is, Person& p);

    > outside of the class declaration but leave it in that header.

    ... then I didn't understood what you meant :)
    You meant to leave the DEFINITION where it is and to FORWARD DECLARE them in the headers that use them. In that case, that works, and at the same time demonstrates how the keyword "extern" is not needed anymore for functions.

     
  • aditsu
    aditsu
    2004-09-22

    ok, just to clarify:
    your original header file was this:

    ifndef PERSON_H

    define PERSON_H

    include <iostream>

    include <string>

    using namespace std;

    class Person
    {
    public:
    string fname;
    string lname;
    string tele;
    friend ostream& operator<<(ostream& os, const Person& p);
    friend istream& operator>>(istream& is, Person& p);
    };

    endif //PERSON_H

    the main point is to change it to this:

    ifndef PERSON_H

    define PERSON_H

    include <iostream>

    include <string>

    using namespace std;

    class Person
    {
    public:
    string fname;
    string lname;
    string tele;
    };

    ostream& operator<<(ostream& os, const Person& p);
    istream& operator>>(istream& is, Person& p);

    endif //PERSON_H

    and leave everything else as is

    alternatively, you can define the operators inline in the header file and remove them from the cpp file:

    inline ostream& operator<<(ostream& os, const Person& p)
    {
    os <<p.fname<<" "<<p.lname<<" "<<p.tele;
    return os;
    }

    (same for >>)

    Adrian

     
  • SoaringFalcon
    SoaringFalcon
    2004-09-22

    Yep yep :) That I knew but I didn't realize that global functions were "explicit" by default so I didn't understand how this could work. Therefore I thought I did it wrong.
    But I also didn't realize that obag (whom help I greatly appreciate) offered the same solution as you and me did. :)

    Anyway, great job everybody.