0

everyone! I am making my own linked list template to both practice and for future use; however, I ran into a problem with one of my functions:

Node* LinkedList::FindNode(int x); //is meant to traverse the list and return a pointer to the containing x as its data.

When trying to declare it in my implementation file, I keep getting messages of Node being undefined and incompatibility errors.

Here is my header file:

#pragma once
using namespace std;

class LinkedList
{
private:
    struct Node 
    {   
        int data;
        Node* next = NULL;
        Node* prev = NULL;
    };

    //may need to typedef struct Node Node; in some compilers

    Node* head;  //points to first node
    Node* tail; //points to last node
    int nodeCount; //counts how many nodes in the list

public:
    LinkedList(); //constructor 
    ~LinkedList(); //destructor
    void AddToFront(int x); //adds node to the beginning of list
    void AddToEnd(int x); //adds node to the end of the list
    void AddSorted(int x); //adds node in a sorted order specified by user
    void RemoveFromFront(); //remove node from front of list; removes head
    void RemoveFromEnd(); //remove node from end of list; removes tail
    void RemoveSorted(int x); //searches for a node with data == x and removes it from list 
    bool IsInList(int x); //returns true if node with (data == x) exists in list
    Node* FindNode(int x); //returns pointer to node with (data == x) if it exists in list
    void PrintNodes(); //traverses through all nodes and prints their data
};

If someone can help me define a function that returns a Node pointer, I would greatly appreciate it!

Thank you!

2
  • 1
    If a public function is supposed to return a Node*, why is Node private? Commented Sep 24, 2014 at 8:28
  • 1
    Are you sure making your own linked list template to "for future use" is a good idea? Why don't you use std::list? Commented Sep 24, 2014 at 8:29

1 Answer 1

3

Since Node is declared within another class, did you remember to include the class name when referring to it in your implementation?

LinkedList::Node *LinkedList::FindNode(int x) { ... }

In the class declaration the prefix isn't required because the declaration is inside the class, and therefore Node is implicitly available.

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.