2
votes

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.

1
Your other functions are actually a part of LinkedList. This one isn't. The name lookup rules change for the same reason you wouldn't want main to be able to do nodePtr temp and have it find your class. - chris
what I can do to fix it? - Jnk

1 Answers

2
votes

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

nodePtr temp = list.head;

to

LinkedList::nodePtr temp = list.head;