Solved

Friend Functions

Posted on 1998-11-02
5
319 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 80 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

759 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now