JordanWx JordanWx - 1 month ago 5
C++ Question

Left of '.' must have class/struct/union in LinkedList Implementation

I have looked all over for the solution for this bug, but i have not been able to find something.

Here is the code that causes the errors:

template<typename DataType>
class LinkedList {
Node<DataType> *headNode;
Node<DataType> *tailNode;
Node<DataType> currentNode;

/// Other functions declared
};

template<typename DataType>
void LinkedList<DataType>::addAtIndex(DataType data, int index)
{
if (index < 0 || index >= size) {
throw LinkedListBoundsException();
}
if (index == 0) {
addToBeginning(data);
} else if(index == size - 1){
addToEnd(data);
} else {
Node<DataType> *nodeBefore = headNode;
Node<DataType> *nodeAfter = tailNode;
Node<DataType> node = Node<DataType>(data);
for (int i = 0; i < index - 1; i++) {
nodeBefore = headNode.getNext();
}
nodeAfter = nodeBefore.getNext();
nodeBefore.setNext(node);
nodeAfter.setPrevious(node);
node.setPrevious(nodeBefore);
node.setNext(nodeAfter);
size++;
}
}


And in my Node.h

#pragma once
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;
template<class DataType>
class Node
{
protected:
Node<DataType> *previous;
Node<DataType> *next;
public:
DataType data;
Node();
Node(DataType newData);
Node(Node<DataType> newPrevious, DataType newData);
Node(DataType newData, Node<DataType> newNext);
Node(Node<DataType> newPrevious, DataType newData, Node<DataType> newNext);
~Node();
void setNext(Node<DataType> newNext);
void setPrevious(Node<DataType> newPrevious);
Node<DataType> getNext();
DataType getCurrent();
Node<DataType> getPrevious();
};

template<class DataType>
Node<DataType>::Node()
{
previous = NULL;
data = DataType();
next = NULL;
}

template<class DataType>
Node<DataType>::Node(DataType newData)
{
previous = NULL;
data = newData;
next = NULL;
}

template<class DataType>
Node<DataType>::Node(Node<DataType> newPrevious, DataType newData)
{
previous = newPrevious;
data = newData;
next = NULL;
}

template<class DataType>
Node<DataType>::Node(DataType newData, Node<DataType> newNext)
{
previous = NULL;
data = newData;
next = newNext;
}

template<class DataType>
Node<DataType>::Node(Node<DataType> newPrevious, DataType newData, Node<DataType> newNext)
{
previous = newPrevious;
data = newData;
next = newNext;
}

template<class DataType>
Node<DataType>::~Node()
{
previous = NULL;
next = NULL;
}

template<class DataType>
void Node<DataType>::setNext(Node<DataType> newNext)
{
next = newNext;
}

template<class DataType>
void Node<DataType>::setPrevious(Node<DataType> newPrevious)
{
previous = newPrevious;
}

template<class DataType>
Node<DataType> Node<DataType>::getNext()
{
return next;
}

template<class DataType>
DataType Node<DataType>::getCurrent()
{
return data;
}

template<class DataType>
Node<DataType> Node<DataType>::getPrevious()
{
return previous;
}


It is worth noting that the error is thrown on all the node/nodeAfter/nodeBefore methods. Also when I try to use all -> instead of . I receive even more erros.

Thank you for the help!

Answer

You've declared the following variables as pointers

Node<DataType> *nodeBefore = headNode;
Node<DataType> *nodeAfter = tailNode;

So to invoke methods from them instead of

nodeBefore = headNode.getNext()

you should use

nodeBefore = headNode->getNext()

and similarly for the other method calls.