Answer the question
In order to leave comments, you need to log in
For the first time I use klion, I don’t understand why namespace didn’t please him?
Good
Even Clion highlights a lot of things in red and I don’t understand what’s wrong
#include <iostream>
using namespace std;
template <namespace T>
class List
{
public:
List();
~List();
void push_back(T data);
private:
template <namespace T>
class Node {
public:
Node * pNext;
T data;
Node(T data = T(), Node * pNext = nullptr) {
this->data = data;
this->pNext = pNext;
}
};
int SIZE;
Node<T> *head;
};
template<namespace T>
List<T>::List()
{
SIZE = 0;
head = nullptr;
};
template<namespace T>
List<T>::~List()
{
};
template<namespace T>
void List<T>::push_back (T data)
{
};
int main() {
system("pause");
return 0;
}
Answer the question
In order to leave comments, you need to log in
As I understand it, you are implementing a singly linked list, the problem is that you cannot pass namespace to the template, and why?
+ In the declaration of the Node class in the template, you have a duplicate variable T, which, in principle, you can not even specify, then the type of the data variable of the Node class will take the type specified by the List class. (I hope you understand this scribble)
#include <iostream>
using namespace std;
template <class T>
class List
{
public:
List();
~List();
void push_back(T data);
private:
class Node {
public:
Node * pNext;
T data;
Node(T data = T(), Node * pNext = nullptr) {
this->data = data;
this->pNext = pNext;
}
};
int SIZE;
Node *head;
};
template<class T>
List<T>::List()
{
SIZE = 0;
head = nullptr;
};
template<class T>
List<T>::~List()
{
};
template<class T>
void List<T>::push_back (T data)
{
};
int main() {
system("pause");
return 0;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question