'=' : cannot convert from 'CircularDoubleDirectedList<int>::Node *' to 'Node *' - c++

I have Node* current where I store a pointer to what node that is current at "top" of the list. When I set a new node as current I get the error:
'=' : cannot convert from 'CircularDoubleDirectedList<int>::Node *' to 'Node *'
while compiling class template member function 'void CircularDoubleDirectedList<int>::addAtCurrent(const T &)' with [ T=int ]
It is the three rows with //Problem comment that generates those errors if take them away everything works fine.
#include "ICircularDoubleDirectedList.h"
template <typename T> class CircularDoubleDirectedList;
class Node;
template <typename T>
class CircularDoubleDirectedList :
public ICircularDoubleDirectedList<T>{
public:
//Variables
Node* current;
int nrOfElements;
direction currentDirection;
//Functions
CircularDoubleDirectedList();
~CircularDoubleDirectedList();
void addAtCurrent(const T& element) override;
private:
class Node
{
public:
T data;
Node* forward;
Node* backward;
Node(const T& element);
};
};
template <typename T>
void CircularDoubleDirectedList<T>::addAtCurrent(const T& element){
Node* newNode = new Node(element);
newNode->data = element;
if (this->nrOfElements == 0){
newNode->forward = newNode;
newNode->backward = newNode;
}
else{
this->current->forward = newNode; // Problem
this->current->forward->backward = newNode; // Problem
}
this->current = newNode; //Problem
}

When you forward declare Node as being outside of the class here:
template <typename T> class CircularDoubleDirectedList;
class Node;
That is declaring a type Node in the global namespace. It is ::Node. Then, within your class declaration, current takes on that type:
template <typename T>
class CircularDoubleDirectedList
: public ICircularDoubleDirectedList<T>
{
public:
Node* current; // this is a pointer to ::Node.
};
Then you provide a declaration of CircularDoubleDirectedList<T>::Node. This is not the same type as ::Node. It also gets looked up first by name resolution rules. So in here:
template <typename T>
void CircularDoubleDirectedList<T>::addAtCurrent(const T& element){
Node* newNode = new Node(element); // newNode is a pointer to
// CircularDoubleDirectedList<T>::Node
But current is a pointer to the still-incomplete type ::Node. Hence the error - you have inadvertently created two types named Node.
If you're going to forward-declare Node, you have to do it inside the class:
template <typename T>
class CircularDoubleDirectedList
: public ICircularDoubleDirectedList<T>
{
class Node; // NOW it's CircularDoubleDirectedList<T>::Node
};

Related

Nested template class constructor

I the compiler can't find the definition of my constructor for the nested class.
My nested class Node is in the middle and the constructor is at the end.
Errors:
error C2244: 'CircularDoubleDirectedList::Node::Node' : unable
to match function definition to an existing declaration see
declaration of 'CircularDoubleDirectedList::Node::Node'
definition
'CircularDoubleDirectedList::Node::Node(const T &)'
existing declarations
'CircularDoubleDirectedList::Node::Node(const T &)'
Code:
#ifndef CIRCULARDOUBLEDIRECTEDLIST_H
#define CIRCULARDOUBLEDIRECTEDLIST_H
#include "ICircularDoubleDirectedList.h"
template <typename T> class CircularDoubleDirectedList;
template <typename T> class Node;
template <typename T>
class CircularDoubleDirectedList :
public ICircularDoubleDirectedList<T>{
public:
//Variabels
Node<T>* current;
int nrOfElements;
direction currentDirection;
//Functions
CircularDoubleDirectedList();
~CircularDoubleDirectedList();
void addAtCurrent(const T& element) override;
private:
template <typename T>
class Node
{
public:
T data;
Node<T>* forward;
Node<T>* backward;
Node(const T& element);// The constructor
};
};
template <typename T>
CircularDoubleDirectedList<T>::CircularDoubleDirectedList(){
this->nrOfElements = 0;
this->current = nullptr;
this->currentDirection = FORWARD;
}
template <typename T>
CircularDoubleDirectedList<T>::~CircularDoubleDirectedList(){
//TODO: Destroy all nodes
}
template <typename T>
void CircularDoubleDirectedList<T>::addAtCurrent(const T& element){
Node<T>* newNode = new Node<T>(element);
newNode->data = element;
if (this->nrOfElements == 0){
newNode->forward = newNode;
newNode->backward = newNode;
}
else{
//this->current->forward = newNode;
//this->current->forward->backward = newNode;
}
//this->current = newNode;
}
template <typename T>
CircularDoubleDirectedList<T>::Node<T>::Node(const T& element){
this->data = element;
}
#endif
First, the forward-declared template <typename T> class Node; is not the same as CircularDoubleDirectedList::Node - the former is a global class template, the latter is a nested class.
Second, you don't need to declare CircularDoubleDirectedList::Node as a template (and if you do, you have to use another template parameter name for it, not T). But as I understand, for this case you should just make it non-template, so:
template <typename T>
class CircularDoubleDirectedList :
public ICircularDoubleDirectedList<T>{
private:
class Node
{
public:
T data;
Node* forward;
Node* backward;
Node(const T& element);// The constructor
};
public:
Node* current;
//...
};
template <typename T>
CircularDoubleDirectedList<T>::Node::Node(const T& element){
this->data = element;
}
You have two class templates named Node, while in reality you want one non-template class named Node. You have forward-declared ::Node<T>, and you have the nested ::CircularDoubleDirectedList<T>::Node<U>.
If you really want it like that, you'll have to add another template keyword to the constructor definition:
template <typename T> //because CircularDoubleDirectedList is a template
template <typename U> //because Node is a template
CircularDoubleDirectedList<T>::Node<U>::Node(const T& element) : data(element)
{}
However, I can't see a single reason to have Node be a template. Inside CircularDoubleDirectedList<T>, do you want to use nodes with type other than T? If not, make Node a normal non-template class:
template <typename T>
class CircularDoubleDirectedList :
public ICircularDoubleDirectedList<T>{
public:
//Variabels
Node<T>* current;
int nrOfElements;
direction currentDirection;
//Functions
CircularDoubleDirectedList();
~CircularDoubleDirectedList();
void addAtCurrent(const T& element) override;
private:
class Node
{
public:
T data;
Node* forward;
Node* backward;
Node(const T& element);// The constructor
};
};
template <typename T>
CircularDoubleDirectedList<T>::Node::Node(const T& element) : data(element)
{}

Template class not inheriting the protected variable from another Template class

I'm making a Linked list which is generic in nature and have some basic functionality. Then I'm trying to make another template class called "Set" which is inheriting from LinkedList. But when I try to access "head" which is a Node<>* defined in Linked List. It's giving an error. My files are:
LinkedList.h
template <typename T>
struct Node {
T data;
Node<T> *next;
};
template <typename T>
class LinkedList {
public:
Node<T>* head;
int size;
LinkedList();
LinkedList(const LinkedList<T> &lst);
~LinkedList();
Node<T>* getHead();
Node<T>* getTail();
};
template <typename T>
class Set:public LinkedList<T> {
public:
void insert(T item);
friend ostream&(ostream& out, const Set<T> set)
};
and an implementation of insert is:
template <typename T>
void Set<T>::insert(T item) {
Node<T>* temp = head;
bool present = false;
while (temp != NULL) {
if (temp->data == item) {
present = true;
}
temp = temp->next;
}
if (present == false) {
/*Node<T> *tail = getTail();
Node<T>* newTail = new Node<T>(item);
newTail->next = NULL;
tail->next = newTail;*/
}
}
It says:
error: "head" was not declared in this scope in line "Node<T>* temp = head"
This C++ oddity is due to two-phase lookup and the fact that head is a dependant name (as a member of a base class that depends on the "current" class's template arguments):
[C++11: 14.6.2/3]: In the definition of a class or class template, if a base class depends on a template-parameter, the base class scope is not examined during unqualified name lookup either at the point of definition of the class template or member or during an instantiation of the class template or member. [..]
Bypass unqualified lookup by introducing this into the expression (per [C++11: 3.4.5]):
Node<T>* temp = this->head;
// ^^^^^^
There's a longer explanation on this previous Stack Overflow answer:
Why do I have to access template base class members through the this pointer?
Here's a minimal(ish) testcase:
#include <iostream>
template <typename T>
struct Base
{
int x = 42;
};
template <typename T>
struct Derived : Base<T>
{
void foo();
};
template <typename T>
void Derived<T>::foo()
{
std::cout << x << '\n';
}
int main()
{
Derived<void> d;
d.foo();
}
// main.cpp: In member function 'void Derived<T>::foo()':
// main.cpp:18:18: error: 'x' was not declared in this scope
// std::cout << x << '\n';
// ^
(live demo)
To fix, change foo thus:
template <typename T>
void Derived<T>::foo()
{
std::cout << this->x << '\n';
}
(live demo)
You're inheriting from a dependent base class so member access needs to be qualified with this:
Node<T>* temp = this->head;
For more information, see this thread.

use of class template requires template argument list, nested classes

I'm trying to implement by own generic/template ArrayList and I've run into a snag. I know the error comes from not having somewhere in the argument list but for me I can't figure it out here, I get a different error if I do. For brevity I've removed functions I'm unable to debug until this one is debugged first.
//ArrayList.h//
#include <iostream>
#include <string>
using namespace std;
template <class T>
class ArrayList {
private:
class Node {
private:
Node* next;
Node* prev;
T* element;
public:
Node();
Node( T* );
Node( Node* /*new prev*/, T* );
~Node();
void setNext( Node* );
Node* getNext();
};
int size;
int maxSize;
int current_index;
Node* myArrayList;
Node* curr;
Node* head;
Node* tail;
public:
};
Implementation of "Node* getNext();" in my cpp file.
//ArrayList.cpp//
#include "arraylist.h"
...
template <class T>
ArrayList::Node* ArrayList::Node::getNext() {
return this->next;
}
Trying to insert behind :: hasn't worked very well... Node* becomes undefined if I put before it.
template <class T>
ArrayList<T>::Node* ArrayList::Node::getNext() {
return this->next;
}
Then I get ";" expected before "*".
Try this:
template <class T>
typename ArrayList<T>::Node* ArrayList<T>::Node::getNext()
{
return this->next;
}
Or in C++11 (Demo):
template <class T>
auto ArrayList<T>::Node::getNext() -> Node*
{
return this->next;
}
or just use an in-line definition, which is recommended for simple accessors.
You need to define your member functions as such :
template <class T>
typename ArrayList<T>::Node* ArrayList<T>::Node::getNext() {
return this->next;
}

error: template argument required for 'struct List'

I'm trying to create my own template for a List class as a learning excercise. I've been having some trouble with template syntax though and i'm now getting the following error message..
main.cpp|Line 8|instantiated from here
error: template argument required for 'struct List'
In function 'int main()':
...
As far as i can tell i'm not misusing anything but this is my first time working with templates and would really appreciate someone looking through and letting me know what i'm doing wrong.
List.hpp:
#if !defined _LIST_HPP_
#define _LIST_HPP_
#include "Node.hpp"
///since we're creating a template everything must be defined in the hpp
template <typename ListType>
class List
{
public:
List();
bool Empty();
void PushFront();
void PushBack();
void PopBack();
Node<ListType>& GetHead();
private:
int _size;
Node<ListType>* _head;
Node<ListType>* _tail;
};
///implement List class here
template <typename ListType>
List<ListType>::List() : _head(0), _tail(0), _size(0)
{
}
template <typename ListType>
bool List<ListType>::Empty()
{
return _size == 0;
}
template <typename ListType>
void List<ListType>::PushFront()
{
_head = new Node<ListType>( _head , 0 );
if (!Empty())
_head->_prev->_next = _head; //set previous nodes _next to new _head
++_size;
}
template <typename ListType>
void List<ListType>::PushBack()
{
_tail = new Node<ListType>( 0 , _tail);
if (!Empty())
_tail->_next->_prev = _tail; // set old tails _prev to new tail
++_size;
}
template <typename ListType>
void List<ListType>::PopBack()
{
}
template <typename ListType>
Node<ListType>& List<ListType>::GetHead()
{
return _head;
}
#endif //define
Node.hpp:
#if !defined _NODE_HPP_
#define _NODE_HPP_
template<typename NodeType>
class Node{
public:
Node( Node* prev = 0, Node* next = 0);
void SetData(NodeType newData);
void GetData();
private:
friend class List;
NodeType _data;
Node* _next;
Node* _prev;
};
///implement Node
template <typename NodeType>
Node<NodeType>::Node(Node* prev, Node* next) : _prev(prev), _next(next)
{}
template <typename NodeType>
void Node<NodeType>::SetData(NodeType newData)
{
_data = newData;
}
template <typename NodeType>
void Node<NodeType>::GetData()
{
return _data;
}
#endif //define
Main.hpp
#include <iostream>
#include "List.hpp"
int main()
{
List<int> testl;
//test
testl.PushFront();
testl.GetHead().SetData(7); //Error thrown here??
std::cout << test1.GetHead().GetData() << std::endl;
return 0;
}
List is a class template, so you need to declare it as such in your friend declaration
template<typename ListType>
friend class List;
If you only want List<NodeType> to be a friend, you need to tell it that template argument, so then the friend declaration becomes
friend class List<NodeType>;
For this to work, it needs to know that List exists as a class template, so you need to forward-declare it at the top of Node.hpp:
template<typename ListType>
class List;

Member functions of a templated class, that take a template type as argument

I have a node struct and stack class. When I put the definition for 'void Push(T data)' outside the class definition I get:
error: 'template<class T> class Stack' used without template parameters
But when I put it inside the class definition it works fine.
Here is the code:
template <class T>
struct Node
{
Node(T data, Node <T> * address) : Data(data), p_Next(address) {}
T Data;
Node <T> * p_Next;
};
template <class T>
class Stack
{
public:
Stack() : size(0) {}
void Push(T data);
T Pop();
private:
int size;
Node <T> * p_first;
Node <T> * p_last;
};
The implementation for Push(T data) is :
void Stack::Push(T data)
{
Node <T> * newNode;
if(size==0)
newNode = new Node <T> (data, NULL);
else
newNode = new Node <T> (data, p_last);
size++;
p_last = newNode;
}
Edit: The solutions worked except that now I get a linking error whenever I try to call the functions.
Stack<int>::Pop", referenced from
_main in main.o
symbol(s) not found.
unless the definitions are in Stack.h instead of Stack.cpp
You need to use the template <class T> again (and then use that T again as the template parameter for the class):
template <class T>
void Stack<T>::Push(T data)
{
Node <T> * newNode;
if(size==0)
newNode = new Node <T> (data, NULL);
else
newNode = new Node <T> (data, p_last);
size++;
p_last = newNode;
}
You need to add a template statement before member functions when defined outside of the class...
template <class T>
void Stack<T>::Push(T data)
{
Node <T> * newNode;
if(size==0)
newNode = new Node <T> (data, NULL);
else
newNode = new Node <T> (data, p_last);
size++;
p_last = newNode;
}
(At least it should be similar to this.)
That's because when the definition is inside the class definition it knows the template parameter. If you want to put the definition outside you need to explicitly tell the compiler that T is a template parameter...
template <class T>
void Stack<T>::Push(T data) {/* code */}