Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Friend Functions

Posted on 1998-11-02
5
Medium Priority
?
374 Views
Last Modified: 2010-04-02
How do you declare friend functions that are not part of the class . 2) How do you declare a friend that has an entire class, example friend class Stack<SE>.
0
Comment
Question by:jermyn
  • 3
5 Comments
 
LVL 11

Accepted Solution

by:
alexo earned 320 total points
ID: 1176774
Here's an example:

    class Class;

    class AnotherClass
    {
    public:
        void FriendMethod(Class& arg);
    };

    class Class
    {
        friend class FriendClass;
        friend void FriendFunc(Class&);
        friend void AnotherClass::FriendMethod(Class&);

    private:
        int privateMemeber;
    };

    void AnotherClass::FriendMethod(Class& arg) { arg.privateMemeber = 3; }

    class FriendClass
    {
    public:
        void Method(Class& arg) { arg.privateMemeber = 2; }
    };

    void FriendFunc(Class& arg) { arg.privateMemeber = 1; }

    void main()
    {
        Class obj;
        AnotherClass anotherObj;
        FriendClass friendObj;

        FriendFunc(obj);
        anotherObj.FriendMethod(obj);
        friendObj.Method(obj);
    }

0
 
LVL 22

Expert Comment

by:nietod
ID: 1176775
Welcome back, alexo!  ronslow is back too.
0
 
LVL 11

Expert Comment

by:alexo
ID: 1176776
Not really back, just a brief visit.  Too much things to do, too little time.
I'm always reachable by email though.
0
 

Author Comment

by:jermyn
ID: 1176777
I'm still kinda lost, I have a declaration:

template < class SE >
class StackNode               // Facilitator class for the Stack class
{
  private:

    // Constructor
    StackNode ( const SE &elem, StackNode *nextPtr );

    // Data members
    SE element;         // Stack element
    StackNode *next;    // Pointer to the next element

  friend class Stack<SE>;
};

template < class SE >
class Stack
{
  public:

    // Constructor
    Stack ( int ignored = 0 );

    // Destructor
    ~Stack ();

    // Stack manipulation operations
    void push ( const SE &newElement );    // Push element
    SE pop ();                             // Pop element
    void clear ();                         // Clear stack

    // Stack status operations
    int empty () const;                    // Stack is empty
    int full () const;                     // Stack is full

    // Output the stack structure -- used in testing/debugging
    void showStructure () const;

    // In-lab operation
    Stack ( const Stack &valueStack );     // Copy constructor

  private:

    // Data member
    StackNode<SE> *top;   // Pointer to the top element
};
and these are som of the error messages I'm getting:

 error C2059: syntax error : '<'
 error C2238: unexpected token(s) preceding ';'
 error C2989: 'Stack' : template class has already been defined as a non-template class
 error C2059: syntax error : '<'
 error C2588: '::~Stack' : illegal global destructor

my question is how do I declare Stack?
0
 
LVL 11

Expert Comment

by:alexo
ID: 1176778
How do you define the member functions?

template <class SE>
Stack<SE>::Stack(int ignored)
{
    // Do something...
}

template <class SE>
Stack<SE>::~Stack()
{
    // Do something...
}

Etc...
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question