C++ access inner class in a template - c++

pls have a look at this code:
template<typename T>
class myclass {
private:
class node {
friend class myclass;
T t;
node(T t_) : t(t_) {};
};
node nd;
node getNode();
public:
myclass(T t_) : nd(node(t_)) {};
T get() {
auto node = getNode();
return node.t;
}
};
template<typename T>
myclass<T>::node myclass<T>::getNode() {
return nd;
}
I define the method getNode() outside the template, and the compiler report this error:
missing 'typename' prior to dependent type name 'myclass<T>::node'
myclass<T>::node myclass<T>::getNode() {
how should I write it correctly?

Need to use typename keyword for dependent type myclass<T>
template<typename T>
typename myclass<T>::node myclass<T>::getNode() {
~~~~~~~~
return nd;
}

Related

template class header file

I'm trying to implement stack data structure using singly linked list.
This is the header file that has template class of Node and List
#pragma once
template <typename E>
class SNode {
E elem;
SNode<E>* next;
public:
friend class SLinkedList<E>;
};
template <typename E>
class SLinkedList {
private:
SNode<E>* head;
public:
SLinkedList();
~SLinkedList();
bool empty() const ();
const E& front() const;
void addFront(const E& e);
void removeFront();
};
template <typename E>
SLinkedList<E>::SLinkedList() : head(NULL) {}
template <typename E>
SLinkedList<E>::~SLinkedList() {
while(!empty()) removeFront();
}
template <typename E>
bool SLinkedList<E>::empty() const {
return head==NULL;
}
template <typename E>
const E& SLinkedList<E>::front() const {
return head->elem;
}
template <typename E>
void SLinkedList<E>::addFront(const E& e) {
SNode<E>* tmp = new SNode<E>;
tmp->elem = e;
tmp->next=head;
head=tmp;
}
template <typename E>
void SLinkedList<E>::removeFront() {
SNode<E>* old = head;
head=head->next;
delete old;
}
This is a simple main code
#include <iostream>
#include "SNode.h"
using namespace std;
int main() {
SNode<int> A(1);
SNode<int> B(2);
SLinkedList<int> L();
L.push(A);
L.push(B);
return 0;
}
When I compile this code the compiler says
SLinkedList is not a class template
I have no idea why it says that since I did put
template
You need to forward declare the SLinkedList templated class. like this:-
template <typename E>
class SLinkedList;
template <typename E>
class SNode {
E elem;
SNode<E>* next;
public:
friend class SLinkedList<E>;
};
...
There are other errors in you code. I hope you are able to resolve them by youself.
You have vexing parse with
SLinkedList<int> L();
Which declares a.. function L, taking no parameters, and returning SLinkedList<int>.
Use
SLinkedList<int> L;
or
SLinkedList<int> L{};

Add a method to templated class for specific type

For example, I have a class template:
template <typename T>
class base {
public:
void set(T data) { data_=data; }
private:
T data_;
};
And for a certain type I would like to add a function, but also have functions from the template class.
template <>
class base<int>{
public:
void set(int data) { data_=data; }
int get(){ return data_;} //function specific to int
private:
int data_;
}
How to do that without copying all members from the template class?
With inheritance:
template <typename T> struct extra {};
template <> struct extra<int> {
public:
int get() const;
};
template <typename T>
class base : public extra<T> {
friend class extra<T>;
public:
void set(T data) { data_=data; }
private:
T data_ = 0;
};
int extra<int>::get() const{ return static_cast<const base<int>*>(this)->data_;}
Demo
You can do this by using enable_if from type_traits to enable the get function only when the template parameter is int. One example is shown below.
#include <type_traits>
template <typename T>
class base {
public:
template <typename X=T,
std::enable_if_t< std::is_same<X,typename T>::value
&& std::is_same<X,int>::value, bool> = false>
int get() { return data_; }
void set(T data) { data_=data; }
private:
T data_;
};

How to call the base constructor if your class derives from unique_ptr

How can do I implement the SearchTree constructor with the T type parameter by calling it's superclass ?
template <class T>
class SearchTree: protected unique_ptr<Node<T> >{
public:
SearchTree<T>();
SearchTree<T>(const T &); //How do I implement this ?
}
template <class T>
class Node{
friend class SearchTree<T>;
public:
Node<T>();
Node<T>(const T & sl_):sl(sl_){};
private:
const T sl;
SearchTree<T> left,right;
}
Inheriting from std::unique_ptr is an instant indicator of a design flaw.
Encapsulation is the way to go. Perhaps start with something like this?
#include <memory>
template<class T> struct Node;
template<class T>
void add_node(std::unique_ptr<Node<T>>& next, T t);
template<class T>
struct Node
{
Node(T t) : _value(std::move(t)) {}
void add(T t)
{
if (t < _value) {
add_node(_left, std::move(t));
}
else if(t > _value) {
add_node(_right, std::move(t));
}
else {
// what?
}
}
T _value;
std::unique_ptr<Node<T>> _left, _right;
};
template<class T>
void add_node(std::unique_ptr<Node<T>>& next, T t)
{
if (next) {
next->add(std::move(t));
}
else {
next = std::make_unique<Node<T>>(std::move(t));
}
}
template<class T>
struct SearchTree
{
void add(T t) {
add_node(_root, std::move(t));
}
std::unique_ptr<Node<T>> _root;
};
int main()
{
SearchTree<int> tree;
tree.add(5);
tree.add(3);
tree.add(4);
}

how to remove error : X is not a class template

I'm no expert in using templates but I'm not sure why I'm getting error: 'SLinked_List' is not a class template: friend class SLinked_List<T>; in the definition of class SNode. What is wrong with this piece of code?
Thank you,
Pranav
#include <iostream>
#include <string>
template <typename T>
class SNode{
friend class SLinked_List<T>;
private:
T data;
SNode<T>* next;
};
template <typename T>
class SLinked_List{
private:
SNode<T>* head;
public:
SLinked_List(){
head = nullptr;
}
bool empty() const { return head == nullptr; }
void insert_first (const T&);
};
template <typename T>
void SLinked_List<T> :: insert_first (const T& t){
SNode<T>* node = new SNode<T>;
node->data = t;
node->next = head;
head = node;
}
int main(){
SLinked_List<std::string> ls;
ls.insert_first("Hello");
return 0;
}
When you use template arguments to refer to a name you are saying that this type already exists as a template and I want to reference a specific specialization of that template. Inside SNode, SLinked_List hasn't been declared yet, so this can't be allowed because the compiler doesn't even know if it's a template yet.
It's clear you want to befriend the specialization taking a T, so you need to declare SLinked_List before SNode:
template <typename T>
class SLinked_List;
template <typename T>
class SNode{
friend class SLinked_List<T>;
private:
T data;
SNode<T>* next;
};
Now the compiler knows that SLinked_List is a template and can be referenced as such.

error: no match for 'operator=' . Trying to inherit from base class and initialize from base class?

I'm trying to declare a base class called BASE and I'm having trouble using the inherited types A and B inside of the BASE class. I'm getting the error
|In member function 'NODE& NODE::operator=(const NODE&)':|
16|warning: no return statement in function returning non-void|
In member function 'void BASE<T, SIZE>::init_A(int) [with T = NODE, unsigned int SIZE = 2u]':|
96|instantiated from here|
39|error: no match for 'operator=' in 'A<NODE, 2u>::DATA[index] = a'|
13|note: candidates are: NODE& NODE::operator=(const NODE&)|
#include <iostream>
class NODE
{
private:
public:
NODE(){}
~NODE(){}
};
template <class T, size_t SIZE>
class A;
template <class T, size_t SIZE>
class BASE
{
protected:
static T DATA[SIZE];
public:
BASE()
{
}
~BASE(){}
void init_A(int index)
{
A<T,SIZE>::DATA[index] = T();
}
};
template <class T, size_t SIZE>
class A : public BASE<T,SIZE>
{
protected:
public:
A(){}
~A(){}
};
template <class T, size_t SIZE>
T BASE<T,SIZE>::DATA[SIZE] = {};
int main()
{
BASE<NODE,2> base;
base.init_A(0);
return 0;
}
I can make it compile, but it might not do what you want.
The first problem is you assignment operator promises to return something and doesn't:
NODE& NODE::operator=(const NODE&)
{
}
Try this
NODE& NODE::operator=(const NODE&)
{
return *this;
}
The second problem is
A<T,SIZE> a;
A<T,SIZE>::DATA[index] = a;
The DATA is an array of T, not an A<T,SIZE>.
Try this
A<T,SIZE>::DATA[index] = T();
Finally you need to declare your statics somewhere.
Finally you need to define your statics somewhere.See here