Solved

Friend Functions

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

740 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