Jnk
Jnk

Reputation: 77

C++ : cannot create node pointer inside ostream& operator <<

I'm using C++ to implement LinkedList, other functions and operators I can create Node* just fine. However when I get to this operator "ostream& operator << (ostream& out, const LinkedList& list)" (output the list), I can not create a temporary inside the operator, can anyone tell me what causes the error and how to fix it?

Here is my LinkedList.h

#ifndef LINKEDLIST_H
#define LINKEDLIST_H

#include <iostream>

using namespace std;

class LinkedList {
    typedef struct Node{
        int data;
        Node* next;

    bool operator < (const Node& node) const {
        return this->data < node.data;
    }

    bool operator <= (const Node& node) const {
        return this->data <= node.data;
    }

    bool operator > (const Node& node) const {
        return this->data > node.data;
    }

    bool operator >= (const Node& node) const {
        return this->data >= node.data;
     }

    friend ostream& operator << (ostream& out, const LinkedList& list);
    friend istream& operator >> (istream& in, const LinkedList& list);

    } * nodePtr;

public: 
    nodePtr head;
    nodePtr curr;
    LinkedList();

   // functions
    void push_front(int);
    void push_back(int);
    int pop_front();
    int pop_back();
    int size();
    bool contains(int);
    void print();
    void clear();

   // overload
    LinkedList& operator =(const LinkedList& list);
    bool operator !=(const LinkedList& list) const;
    LinkedList operator +(const int v) const;
    LinkedList operator +(const LinkedList& list) const;
    LinkedList operator - (const int v) const;   
    friend ostream& operator << (ostream& out, const LinkedList& list);
    friend istream& operator >> (istream& in, const LinkedList& list);  
   };

 #endif /* LINKEDLIST_H */

in my LinkedList.cpp:

ostream& operator << (ostream& out, const LinkedList& list) {
    nodePtr temp = list.head;          <----------------- **Unable to resolve identifier nodePtr**
}

I can create Node* (nodePtr) on my other functions just fine.

Upvotes: 2

Views: 143

Answers (1)

songyuanyao
songyuanyao

Reputation: 172934

nodePtr is defined inside LinkedList, it need to be qualified. Change

nodePtr temp = list.head;

to

LinkedList::nodePtr temp = list.head;

Upvotes: 2

Related Questions