Thread: Please help needed with overloading the insertion operator

  1. #1
    Registered User
    Join Date
    Feb 2015
    Posts
    25

    Please help needed with overloading the insertion operator

    I am trying to overload the insertion operator for a class linked list which is declared as a friend of class node




    Code:
    the errors I get are:
    
    'Linkedlist::head' : cannot access private member declared in class 'Linkedlist'
    'Node::value' : cannot access private member declared in class 'Node'
    Node::next' : cannot access private member declared in class 'Node'
    
    the code begins here:
    
    ostream& operator<<(ostream out,const Linkedlist& List1)
     
     {
         Node* current=List1.head;
        while(current)
        
        {
            out<<current->value<<;
            current=current->next;
        }
        return out;
     }
    Last edited by shina; 07-22-2015 at 12:50 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The problem is that you are accessing private member variables of the LinkedList and Node classes, but this overloaded operator<< is not a friend function of these classes.

    If such an interface does not already exist, I suggest that you add two public member functions to the Node class: one to access the value and another to return the next pointer. Then, you can use these public member functions from within this overloaded operator<< implementation.

    After you have done this, you would still be accessing the private member variable named head of the LinkedList class. To fix this, it would be appropriate to declare this overloaded operator<< as a friend function of the LinkedList class.

    By the way, this is a syntax error:
    Code:
    out<<current->value<<;
    You probably wrote or wanted to write:
    Code:
    out << current->value;
    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
    Oct 2006
    Posts
    3,445
    I suspect your declaration is different from your definition. For example, out should be a reference to an ostream, not an ostream value.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  4. #4
    Registered User
    Join Date
    Feb 2015
    Posts
    25
    yes, that is what I meant.thanks for pointing it out.

  5. #5
    Registered User
    Join Date
    Feb 2015
    Posts
    25
    The function that overloads the << operator is a friend function in class linked list.

  6. #6
    Registered User
    Join Date
    Feb 2015
    Posts
    25
    Quote Originally Posted by Elkvis View Post
    I suspect your declaration is different from your definition. For example, out should be a reference to an ostream, not an ostream value.
    yes, you are right.I had done that in the header file but forgot to do it in the implementation one. Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. insertion operator
    By Aisthesis in forum C++ Programming
    Replies: 1
    Last Post: 05-23-2010, 04:42 AM
  2. Trouble with overloading Insertion Operator
    By adamdavis in forum C++ Programming
    Replies: 2
    Last Post: 01-23-2010, 06:25 PM
  3. insertion operator question
    By tsubasa in forum C++ Programming
    Replies: 5
    Last Post: 06-08-2006, 01:47 PM
  4. overloading extraction and insertion
    By brianptodd in forum C++ Programming
    Replies: 3
    Last Post: 11-05-2003, 06:16 PM
  5. Overloading insertion and extraction problem
    By Curwa in forum C++ Programming
    Replies: 1
    Last Post: 01-15-2003, 09:20 PM