2

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.

2
  • 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. Commented Sep 28, 2014 at 2:45
  • what I can do to fix it? Commented Sep 28, 2014 at 2:51

1 Answer 1

2

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

nodePtr temp = list.head;

to

LinkedList::nodePtr temp = list.head;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.