Thread: Help to understand the friendship of functions related to classes

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    257
    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,815
    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%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    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
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  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, 01: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