0

I am trying to write a linked list class with some basic features like add node, delete a node and search recursively in the list. I have defined the head of the list as a private variable but I need to access it for the recursive search function so I tried to define a GetHead() function that will return the pointer to head. However I am having some trouble with compiling it in NetBeans.

Here is the class header

class List{
private:

    typedef struct node{
        int data;
        node* next;
    }*nodePtr;

    nodePtr head;
    nodePtr curr;
    nodePtr temp;

public: 
    List();
    void AddNode(int addData);
    void DelNode(int delData);
    void PrintList();
    void SearchRecursive(nodePtr Ptr, int searchVal);
    nodePtr GetHead();
};

The GetHead() function is as follows:

nodePtr List::GetHead(){
    return head;
}

When I compile, I get

error: unknown type name 'nodePtr'
error: cannot initialize return object of type 'int' 
       with an lvalue of  type 'nodePtr' (aka 'List::node *')

Is there a problem in how I am returning the pointer to the struct node?

3
  • nodePtr GetHead(); This won't work for public access scope, since nodePtr is declared in the private class section. Commented Jan 23, 2015 at 16:50
  • @πάνταῥεῖ Actually, it works if you use auto. As long as you don't name the private thing... Commented Jan 23, 2015 at 17:20
  • @juanchopanza Ah, THX! Good to know, didn't think about auto. Commented Jan 23, 2015 at 17:21

2 Answers 2

4

nodePtr is defined in List, so you need the right scope:

List::nodePtr List::GetHead()
^^^^^^
Sign up to request clarification or add additional context in comments.

3 Comments

Sure? The nodePtr typedef is class private, while GetHead() is public.
@πάνταῥεῖ: That doesn't stop you defining the function, it just makes it slightly inconvenient to use.
That seemed to fix it. Thanks!
0

There are two ways to get it compiling:

1)You have defined a struct inside a class in private scope....That hides it from the rest of the world. But as juanchopanza pointed out, you need to specify the scope.

2)Another way to do it would be to take the definition of node outside the class and it should work without changing the function prototype.

First one would be better because the rest of the world has no use for it apart from your linked list class.

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.