Initialization of a struct defined in a templated parent class - c++

I have defined a struct within the protected section of a parent class, which I would like to use in an inherited class.
This works as expected if the parent/child classes aren't templated classes. But does not compile as-is below.
Specifically, the compiler (clang 8.0.1) reports:
inheritance_example.cpp:33:26: error: unknown type name 'Node'
this->head = new Node(toAdd);
From what I have read, I am guessing that the template type specification isn't being assigned to Node, and is thus not being found by the inherited class, but trying the fixes I have found in that vein (i.e. add something along the lines of using Parent<T>::Node, or add a type specifier to the call to the Node constructor), have not worked for me.
Any ideas on how to fix this issue?
#include<iostream>
template <class T>
class Parent
{
protected:
struct Node
{
Node(int value)
{
this->data = value;
this->next = nullptr;
};
~Node() {};
Node* next;
int data;
};
Node* head;
public:
Parent() {};
~Parent() {};
};
template <class T>
class Child : Parent<T>
{
public:
Child()
{
this->head = nullptr;
};
~Child()
{
delete this->head;
this->head = nullptr;
};
void dummyAdd(T toAdd) {
this->head = new Node(toAdd);
};
void dummyPrint()
{
std::cout << this->head->data << std::endl;
};
};
int main()
{
Child<int> t;
t.dummyAdd(5);
t.dummyPrint();
return 0;
}

In order to pack the comment into an answer!
Node is a depended name and hence you need to use keyword typename there. Meaning in the dummyAdd function, you need
void dummyAdd(T toAdd)
{
this->head = new typename Parent<T>::Node(toAdd);
// ^^^^^^^^^^^^^^^^^^^^
};
That is, however, a bit verbose/ more typing. Therefore providing a type alias for Node in the Child will be a good idea.
template <class T>
class Child : Parent<T>
{
using Node = typename Parent<T>::Node; // template type alias
public:
void dummyAdd(T toAdd)
{
this->head = new Node(toAdd); // now you can this
};
// other code...
};

Related

Redefinition as different kind of symbol using friend class C++14

I am trying to write a template for a class. This class uses a Node class within it so I have defined the Node class as having the List class as a friend. See below.
template <typename T>
class Node {
private:
Node() {
next = nullptr;
prev = nullptr;
}
Node(int data) : Node() { this->data = data; }
Node *next;
Node *prev;
int data;
friend class DoubleLinkedList;
};
template<typename T>
class DoubleLinkedList {
public:
DoubleLinkedList();
~DoubleLinkedList();
private:
Node *_head;
Node *_tail;
};
I have another file where the classes are implemented. I get this error with or without the template definition above the List class. Can someone explain this?
This works for me:
template <typename T>
class Node
{
private:
Node()
{
next = nullptr;
prev = nullptr;
}
Node(int data) : Node() { this->data = data; }
Node *next;
Node *prev;
int data;
template<typename T> friend class DoubleLinkedList;
};
template<typename T>
class DoubleLinkedList
{
public:
DoubleLinkedList();
~DoubleLinkedList();
private:
Node<T> *_head;
Node<T> *_tail;
};

How can I access a child class member stored in a parent class?

I was assigned to create a sparse matrix. In the process, I got into a problem. I realized I could not access a member of a child class that was stored in a parent class.
I 'googled' my problem and what I got was that there were some casting problems. I tried it out and did not work either.
Sample code:
main.cpp
#include <iostream>
template <typename T>
class Node
{
public: // public for example purposes
Node<T> *down, *right;
public:
explicit Node(Node<T> *d, Node<T> *r) : down(d), right(r) {}
};
template <typename T>
class HNode : public Node<T>
{
private:
unsigned idxValue;
public:
HNode(unsigned iv) : idxValue(iv), Node<T>(nullptr, nullptr) {}
};
template <typename T>
class ENode : public Node<T>
{
public: // public for example purposes
unsigned row;
unsigned col;
int value;
public:
ENode(unsigned r, unsigned c, int v) : row(r), col(c), value(v), Node<T>(nullptr, nullptr)
{}
};
int main(void)
{
Node<int> *root;
root = new Node<int>(nullptr, nullptr);
root->right = new HNode<int>(0);
root->down = new HNode<int>(0);
root->right->down = new ENode<int>(0, 0, 10);
std::cout << root->right->down->value << '\n';
}
This is the error I get:
error: no member named 'value' in 'Node<int>'
std::cout << root->right->down->value << '\n';
root is a Node pointer. root->right is another Node pointer. root->right->down is--you guessed it--a Node pointer.
When you do [...]->value, you're dereferencing that Node pointer, which gives you a Node, and then trying to get the value member on it. But Node has no value member!
You could attempt to cast your Node pointer into an ENode pointer, using dynamic cast. Which would look like:
Node *n = root->right->down;
if(ENode *en = dynamic_cast<ENode*>(n)) {
std::cout << en->value << std::endl;
} else {
std::cout << "That's not an ENode!" << std::endl;
}
But to do this, you'll need to make Node polymorphic (which you can see detailed here).
Also note that in production code, you should be checking to make sure root, root->right and root->right->down are all non-null before going ahead and dereferencing them.
The root class Node only has pointers to other Node's it does not know about what derived classes they might be.
It is "usually" best to have the correct interface in the base class to get results/values from the different kind of derived classes.
So for example if you have a base class animal:
class animal
{
virtual int number_of_limbs() = 0;
}
Then the derived class pig:
class pig: public animal
{
int number_of_limbs() override { return 3;}
}
By doing this the "interface" to the class is generic but each derived/specialisation can have it specific value.
In your case you probably just need a function called virtual int get_value() in your base class and then implement that in your ENode class...
(note all code above is pseudo code only)

Inheritance with Nodes in C++

I'm writing a doubly linked list in C++ and have a class Node which I'm using for a singly linked list. Below shows the definition of the class.
Node.h
#ifndef NODE_H
#define NODE_H
template <class T>
class Node {
public:
Node<T>() { next = nullptr; }
Node<T>(T init) { data = init; next = nullptr; }
void setData(T newData) { data = newData; }
void setNext(Node<T> *nextNode) { next = nextNode; }
const T getData() { return data; }
Node<T> *getNext() { return next; }
private:
T data;
Node<T> *next;
};
#endif
Obviously the main difference between a singly linked list and doubly linked list is a pointer to the previous Node, so I'm trying to inherit everything from the Node class in a new class and simply add a prev attribute:
DoublyLinkedList.h
#ifndef DOUBLY_LINKEDLIST_H
#define DOUBLY_LINKEDLIST_H
#include "Node.h"
template <class T>
class DLLNode : public Node {
public:
// Inherit default constructor from Node and set prev to nullptr;
DLLNode<T>() : Node<T>(), prev() {}
// Inherit constructor from Node and set prev to nullptr;
DLLNode<T>(T init) : Node<T>(init), prev() {}
Node<T> *getPrev() { return prev; }
private:
Node<T> *prev;
};
/*
TODO: Implement doubly linked list class
*/
#endif
My driver is, simply, the following:
driver.cc
#include <iostream>
#include "DoublyLinkedList.h"
int main()
{
DLLNode<int> test;
return 0;
}
When I compile, I get the following errors:
./DoublyLinkedList.h:7:24: error: expected class name
class DLLNode : public Node {
^
./DoublyLinkedList.h:9:18: error: type 'Node<int>' is not a direct or virtual base of 'DLLNode<int>'
DLLNode<T>() : Node<T>(), prev() {}
^~~~~~~
driver.cc:6:15: note: in instantiation of member function 'DLLNode<int>::DLLNode' requested here
DLLNode<int> test;
I don't understand why the class Node isn't being recognized as a class as my compiler has claimed by the first error. Any tips would be greatly appreciated.
My compiler is Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn)
You need to pass the template type parameter to your templated base class, when inheriting:
template <typename T>
class DLLNode : public Node<T> {
// ^^^
// ...
};

Using template class inside another class

I am trying to create a queue which will use generic item. I am receiving an error with the following code.
How to use the template class inside another class?
Here is what I have tried so far:
#include <iostream>
using namespace std;
template<class T>
class Item
{
public:
Item(const T & item)
: itemVal(item)
{
}
private:
T itemVal;
};
class MyQueue
{
public:
// Error #1
void InsertNode(const Item & item);
private:
struct Node {
// Error #2
Item item;
struct Node * next;
};
};
int main()
{
Item<int> * element = new Item<int>(9);
return 0;
}
Item is not a type, it is a class template. You need to provide the template parameter. In this case, int:
void InsertNode(const Item<int> & item)
and
struct Node{
Item<int> item;
Node<int> * next;
};
Otherwise, you can make MyQueue and Node class templates.
It would be better to redesign your class.
template<class T>
class MyQueue {
struct Node {
T item;
Node * next;
};
public:
MyQueue();
void InsertNode(const T & item);
private:
Node * _root;
};
P.S. Sorry for my English.

Holding a generic type's instance - C++

I have a tree_node class and a tree class.
template<typename T>
class tree_node
{
public:
tree_node(const std::string& key_, const T& value_)
: key(key_), value(value_)
{
}
private:
T value;
std::string key;
};
template<typename T>
class tree
{
public:
tree() : root(new tree_node<T>("", ???)) { }
private:
tree_node<T>* root;
};
tree_node expects an instance of T when creating. How can I pass it in the ??? place? I can say T(), but it will work only if T has a parameterless constructor. I can't have a parameterless constructor for tree_node as it won't compile if T doesn't have a parameterless constructor.
I am looking for a way to design tree_node which can hold all types correctly including pointer types.
Edit
After trying various methods, I found that boost::optional is helpful in this case. I can make the T value into boost::optional<T> value. This will solve the empty constructor issue. So I can have another constructor overload of tree_node which just takes a key. This can be used by the root node. Is this the correct way to go?
Thanks..
Init root value should be zero. If you push new node you obviously know value.
template<typename T>
class tree
{
public:
tree() : root(0) { }
void push (const std::string& key, const T & t) {
if (root == 0) {
root = new tree_node<T>(key, t);
} else {
// Make complex tree
}
}
private:
tree_node<T>* root;
};
Add
If you use suffix tree you should make two types of vertices:
enum NodeType { EMPTY_NODE, VALUE_NODE };
class base_tree_node
{
public:
base_tree_node() :parent(0), left(0), right(0) {}
virtual NodeType gettype() = 0;
protected:
base_tree_node* parent;
base_tree_node* left;
base_tree_node* right;
};
class empty_tree_node : base_tree_node
{
virtual NodeType gettype() { return EMPTY_NODE; }
}
template<typename T>
class tree_node : base_tree_node
{
public:
tree_node(const std::string& key_, const T& value_)
: key(key_), value(value_)
{
}
virtual NodeType gettype() { return VALUE_NODE; }
private:
T value;
std::string key;
};
tree( const T & t ) : root(new tree_node<T>("", t )) { }
I have once done a linked list (just for fun) which needed a sentinel node not meant to hold any data, and I had the following structure:
struct BaseNode
{
BaseNode* next;
BaseNode(BaseNode* next): next(next) {}
};
template <class T>
struct Node: public BaseNode
{
T data;
Node(const T& data, BaseNode* next): BaseNode(next), data(data) {}
};
template <class T>
struct List
{
BaseNode* head;
List(): head(new BaseNode(0)) {}
void add(const T& value)
{
Node<T>* new_node = new Node<T>(value, head->next);
head->next = new_node;
}
T& get_first()
{
assert(head->next);
return static_cast<Node<T>*>(head->next)->data;
}
//...
};
The class itself must make sure it gets necessary casts right and doesn't try to cast head or root itself to Node<T>.
A tree node should have (or be) a collection of child nodes. A tree should have (or be) a collection of root nodes. Both those collections should be the same type. Very simply:
template <class T>
class NodeCollection
{
std::vector<Node<T> *> nodes;
public:
// any operations on collection of nodes
// copy ctor and destructor a must!
};
template <class T>
class Node : public NodeCollection<T>
{
T value;
public:
// ctor
// access to value
};
template <class T>
class Tree : public NodeCollection<T>
{
public:
// ctor
};
This way the shared definition of Tree and Node is actually in NodeCollection, and so Tree doesn't need to carry a dummy value.