Domn Werner
Domn Werner

Reputation: 602

Function to Return Node Pointer

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!

Upvotes: 0

Views: 13295

Answers (1)

Matti Virkkunen
Matti Virkkunen

Reputation: 65126

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.

Upvotes: 3

Related Questions