Solved

Friend Functions

Posted on 1998-11-02
5
349 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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.
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