Reputation: 970
So what I am trying to do is I want to have a class LinkedList
and in the header file I have a protected struct ListNode
that has a string value parameter and a pointer to the next node. In my public member functions I have a add function that calls a private add function that is recursive.
Here is the H file.
#ifndef LINKEDLIST_H_INCLUDED
#define LINKEDLIST_H_INCLUDED
#include <iostream>
using namespace std;
class LinkedList
{
protected: // Declare a class for the list node.
struct ListNode
{
string value;
ListNode *next;
ListNode(string value1, ListNode *next1 = NULL)
{
value = value1;
next = next1;
}
};
ListNode *head; // List head pointer
public:
LinkedList() { head = NULL; } // Constructor
~LinkedList(); // Destructor
void add(string value) { head = add(head, value);}
private:
// Recursive implementations
ListNode *add(ListNode *aList, string value); //Function called by the void add function in public
};
#endif // LINKEDLIST_H_INCLUDED
The problem arises when I try to implement this recursive add function. I have tried the code below but I keep getting an error that ListNode does not have a type. Even though I have defined it as a protected attribute?
Here is the Implementation file.
#include "LinkedList.h"
//Destructor works fine, nothing wrong here and we are using ListNode
LinkedList::~LinkedList()
{ ListNode *garbage;
head = garbage;
delete garbage;
}
//Error comes from this recursive add function
ListNode LinkedList::*add(ListNode *aList, string val)
{
//add a node recursively
}
Any help on what I need to change is Greatly Appreciated.
(note) if you need anymore information I would gladly post it. I just want some help. Thank you!
Upvotes: 2
Views: 65
Reputation: 8598
The type is LinkedList::ListNode
, not ListNode
. Using only ListNode
inside the LinkedList
class and its methods works, because the context is clear, but once you are outside of it you must write the full type.
This should work:
LinkedList::ListNode* LinkedList::add(ListNode* aList, string val)
{
//...
}
Upvotes: 1
Reputation: 41092
LinkedList::ListNode* LinkedList::add(ListNode *aList, string val)
You need to properly scope ListNode*
as under LinkedList
, since it's declared within the class declaration.
Upvotes: 3