Overloading >> as a Friend in a Linked List

I'm trying to read a list of integers from a file into a linked list using nodes. I want to overload the >> operator as a friend function to read the data. I also want to overload the << operator as a friend function to output the data (to the screen for now).

I think I may have the function for the overloaded >> operator coded correctly, but the program has yet to compile without errors, so I can't be sure. Currently, I'm getting an error that says: "'data' has not been declared" with respect to the overloaded << function.

What am I doing wrong?

Thanks.


Here's the main program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// FILE NAME: ListDemo.cpp


#include <iostream>
#include <fstream>
#include "myList.h"

using namespace std;
using namespace A;

int main()
{
    // create lists
    myList list1;
    
	// declare stream objects
    ifstream inData;
    ofstream outData;
    
    // open files
    inData.open("list1.txt");
    outData.open("outlist1.txt");
    
    // test for valid files
    if (!inData)
    {
          cout << "ERROR. Cannot open the file 'list1.txt' << endl;
          return 1;
    }
    
    if (!outData)
    {
          cout << "ERROR. Cannot open the file 'outlist.txt'." << endl;
          return 1;
    }

    inData >> list1;
    
    while (inData)
    {
        cout << list1;
        
        inData >> list1;
        
    }
    
    inData.close();
    outData.close();    
    
    // Keep the window open...
    cin.ignore(256, '\n');
    cout << "\n\n\nPress ENTER to continue..." << endl;
    cin.get();
    // End open-window code

    return 0;
}		 


Here's how I have my friend functions coded so far in my implementation file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
          
    ifstream& operator >>(ifstream& in, myList& value)
    // FRIEND FUNCTION
    {
        in >> value.component;
        value.insert(value.component);
        
        return in;
    }
    
    
    
    ostream& operator <<(ostream& out, const myList& outof)
    // FRIEND FUNCTION
    {  
        node* node_ptr = outof.head_ptr;  // Loop control pointer
        
        while( node_ptr->data() != NULL )
        {
            out << node_ptr->data() << endl;
            node_ptr = node_ptr.data()->link();
        }
        
        return out;
    }


Here's the 'insert' function I'm trying to call from the friend function:
1
2
3
4
5
void myList::insert( const value_type& entry )
    // MODIFICATION MEMBER FUNCTION
    {
        list_head_insert( head_ptr, entry );
    }


Here's the node class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// FILE NAME: node.h

#ifndef NODE_A
#define NODE_A

#include <cstdlib> // for size_t and NULL

using namespace std;

namespace A
{
    class node
    {
        public:
            // TYPEDEF
            typedef int value_type;
            
            // CONSTRUCTOR
            node ( const value_type& init_data = value_type(),
                            node* init_link = NULL )
                { data_field = init_data; link_field = init_link; }
            
            // Member functions to set the data and link fields:
            void set_data ( const value_type& new_data) { data_field = new_data; }
            void set_link ( node* new_link) { link_field = new_link; }
            
            // Constant member function to retrieve the current data:
            value_type data() const { return data_field; }
            
            // Two slightly different member functions to retrieve the current
            // link:
            const node* link() const { return link_field; }
            node* link()  { return link_field; }
            
        private:
            value_type data_field;
            node* link_field;            
    };
    
    // FUNCTIONS for the linked-list toolkit    
    size_t list_length( const node* head_ptr );
    void list_head_insert( node*& head_ptr, const node::value_type& entry);
    void list_insert( node* previous_ptr, const node::value_type& entry );
    node* list_search( node* head_ptr, const node::value_type& target );
    const node* list_search
        ( const node* head_ptr, const node::value_type& target );
    node* list_locate( node* head_ptr, size_t position );
    const node* list_locate( const node* head_ptr, size_t position);
    void list_head_remove( node*& head_ptr );
    void list_remove( node* previous_ptr );
    void list_clear( node*& head_ptr);
    void list_copy( const node* source_ptr, node*& head_ptr, node*& tail_ptr );
} 

#endif 


Here's the implementation of the 'list_head_insert' function:
1
2
3
4
void list_head_insert( node*& head_ptr, const node::value_type& entry)
    {
        head_ptr = new node( entry, head_ptr );
    }
I think it means that the variable data has not been declared and reading through your code I cant find the variable either, and what your using looks an awful lot like a function call.

>
1
2
3
4
5
while( node_ptr->data() != NULL )
        {
            out << node_ptr->data() << endl;
            node_ptr = node_ptr.data()->link();
        }
Last edited on
Topic archived. No new replies allowed.