Solved

Friend Functions

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Title # Comments Views Activity
FMX StringGrid1->Canvas->FillRect Problem 3 170
Socket Programming (Unix) 8 141
passing data from one form to another form in c++ 27 82
How do i run a c++ file? 15 46
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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

856 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