Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!cornell!uw-beaver!tektronix!reed!psu-cs!karenc From: karenc@psu-cs.UUCP (Karen Cunningham) Newsgroups: comp.lang.c++ Subject: Problem with member functions as friends Message-ID: <1623@psu-cs.UUCP> Date: 8 Feb 89 18:55:19 GMT Distribution: usa Organization: Dept. of Computer Science, Portland State University; Portland OR Lines: 65 I am teaching an object-oriented programming class in which I'm including a section on c++. I wanted to show several different ways of allowing (selected parts of) a derived class access to private data of the base class. I succeeded in making several examples work, but had trouble with one. I tried to make a member function of the derived class a friend of the base class. This seems to be the same as the example from page 195 in Stroustrup. I tried to compile that example (it is included below) on several different c++ compilers without success. The message I get occurs at the last line of class employee and states that class manager is undefined. Is this a bug or am I missing something? Karen Cunningham Usenet: {ucbvax,ihnp4,uunet,hplabs}!tektronix!psu-cs!karenc CSNET: karen@cs.pdx.edu InterNet: karen%cs.pdx.edu@relay.cs.net USMail: Karen Cunningham; Computer Science Department; Portland State University; P.O. Box 751; Portland, OR 97207 Phone: (503) 464-4039 ------------------------------Example Follows--------------------------- #include class manager; // This is the example from Stroustrup, page 195. // The second example works (making class manager a friend), // however the first example does not work (making function // "void manager::print" a friend). // // I've tried defining a forward reference to manager::print, // and then making it a friend to employee that didn't work either. class employee { friend void manager::print(); // making this "friend class manager;" works char* name; // ... public: employee* next; employee(char* s) {name = s;} void print() {cout << " employee name is "<< name << "\n";} // ... }; class manager : public employee { // ... public: manager(char* s) : (s) {;} void print(); // ... }; void manager::print() { cout << " manager name is " << name << "\n"; // ... } main() { manager mgr("Hugh"); mgr.print(); }