Help to understand the friendship of functions related to classes

This is a discussion on Help to understand the friendship of functions related to classes within the C++ Programming forums, part of the General Programming Boards category; Hi. I'm developing a stack using a linked list. I have to develop two friend functions to do something with ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    26

    Post Help to understand the friendship of functions related to classes

    Hi. I'm developing a stack using a linked list.

    I have to develop two friend functions to do something with the list.

    But I've tried to understand the friendship, without any consistent result.

    Extract of code:

    Code:
    class Element {
        
            public:
                int Data;
                Element *nextElement;
                Element(int Element) {Data = Element; nextElement = NULL;}
                ~Element() { }
                
    };
    
    class Stack {
        
        private:    
            Element *firstElement;
        
        public:
            Stack() { firstElement = NULL; }
            ~Stack() { }
            bool isEmpty() { return firstElement == NULL; }
            void push(int El)
            {
                Element *pElement = new Element(El);
                pElement->nextElement = firstElement;
                firstElement = pElement;
            }
            int pop()
            {
                if (isEmpty())
                {
                    cout << "Empty stack";
                    exit(EXIT_FAILURE);
                }
                else
                {
                    Element *pElement = firstElement;
                    firstElement = firstElement->nextElement;
                    int temp = pElement->Data;
                    delete pElement;
                    return temp;
                }
            }
            
            void show()
            {
                Element *pElement = firstElement;
                while(pElement != NULL)
                {
                    cout << pElement->Data << "\n";
                    pElement = pElement->nextElement;
                }
            }
            
            friend int ::count(Stack& stack);
            friend void ::extract(Stack& stack);
    
    };
    When I try to define the friend function outside the class in order to do something (e.g. count the element of the stack) I get errors.

    I try to define them this way:

    Code:
    int Stack::count(Stack& stack)
    {
        //Do something
    }
    I have: Method count does not have declaration.

    If I declare like:

    Code:
    int count(Stack& stack)
    {
        //Do something
    }
    I have no errors, but I cannot access the private properties of the class.

    The thing I would like to do is: sum the elements of the stack with a friend function.

    Please help me understand how it works.

    Thank you.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    You probably forgot to declare count to be a friend.

    Personally, I think it should be a member function instead, in which case it should be:
    Code:
    int Stack::count() const
    {
        //Do something
    }
    Though I note that size is a more canonical name for such a member function, and indeed count risks a conflict with std::count if you go around using namespace std indiscriminately.

    Quote Originally Posted by ferenczi
    The thing I would like to do is: sum the elements of the stack with a friend function.
    I do not think that summing the elements of a stack should be part of the interface of a stack. If you want to sum the elements of a stack, pop the elements one by one to sum it (e.g., in a non-member non-friend function). If that is inappropriate, then perhaps a stack is inappropriate to begin with.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    26
    I have to do that way because it's an exercise.

  4. #4
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    227
    Quote Originally Posted by ferenczi View Post
    Code:
    class Stack {
        public:
            int pop()
            {
                if (isEmpty())
                {
                    cout << "Empty stack";
                    exit(EXIT_FAILURE);
                }
                else
                {
                    Element *pElement = firstElement;
                    firstElement = firstElement->nextElement;
                    int temp = pElement->Data;
                    delete pElement;
                    return temp;
                }
            }
    };
    This is usually not a good idea. A library class / function should never just summarily terminate the process in case of an error. The proper way to inform client code about failure of an operation is either through exceptions or return values.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    26
    Thank you for the tip, but it doesn't concern the thread.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,157
    Not withstanding laserlight's legitimate concern that the function should be a member the second version of the function count() is the correct one.

    It will need to access members of the argument (for example using stack.firstElement. It cannot access a private member (like firstElement) of a class without knowing what object that member belongs to.

    The colons in the friend declaration are possible superfluous in your case. If you don't know what they actually mean, leave them out.
    Right 98% of the time, and don't care about the other 3%.

  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,144
    I have no errors, but I cannot access the private properties of the class.
    O_o

    The `friend' statements says that you can access `private' components.

    I imagine this is an issue born of not copying and pasting what you actually have tried.

    What have you actually done?

    Soma
    “Often out of periods of losing come the greatest strivings toward a new winning streak.” -- Fred Rogers
    “Salem Was Wrong!” -- Pedant Necromancer

  8. #8
    Registered User
    Join Date
    Feb 2008
    Posts
    26
    Now they work.
    I've removed the colons and used the second version for the function.
    Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 09-04-2011, 08:34 PM
  2. Friendship declaration?
    By 843 in forum C++ Programming
    Replies: 6
    Last Post: 04-10-2011, 03:29 AM
  3. class friendship.
    By nimitzhunter in forum C++ Programming
    Replies: 2
    Last Post: 11-16-2010, 12:04 AM
  4. Replies: 21
    Last Post: 04-25-2010, 11:32 AM
  5. Passing structs to callback functions (GTK+ related)
    By Raskalnikov in forum C Programming
    Replies: 2
    Last Post: 03-21-2009, 12:46 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21