Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
There are many errors in my codes.
But i don't know about wrong things...
There are common error massages such that "invalid use of template-name
‘node’ without an argument list", "‘head_ptr’ was not declared in this
scope", "‘tail_ptr’ was not declared in this scope",
"‘t’ was not declared in this scope" ,
"template argument 1 is invalid", "expected type-specifier before ‘Node’"
I don't think my overall code is wrong.
But too many error make me to think
all of composition of coding is error..
It is a part of all code.
error explanation
template <typename T>
Node* Node<t>::getNext(void)
{ return next; }
template <typename T>
class List
{
private:
Node* head_ptr; Node* tail_ptr; int numOfItems;
public:
List(); //constructor
int size(void); bool isEmpty(void);
void insertTail(T x);
void removeHead(void);
Node<T>* getHead(void);
Node<T>* getTail(void);
void insert_with_priority(T x);
};
template <typename T>
List<T>::List()
{ head_ptr = NULL; tail_ptr = NULL; numOfItems = 0; }
template <typename T>
void List<T>::insertTail(T x){
Node<t>* newTail = new Node(x);
tail_ptr->setNext(newTail);
tail_ptr = newTail;
numOfItems++;
}
template <typename T>
void List<T>::removeHead(void){
if(numOfItems == 0)
return 0;
if(numOfItems == 1){ //i.e. headptr == tail_ptr
delete head_ptr; head_ptr = NULL; tail_ptr = NULL;
'
Please give me many feedback.
Even though your question is incomplete, I'll help you with one of the errors (and it might solve other follow-up errors as well)...
Lets take the lines
template <typename T>
Node* Node<t>::getNext(void)
{ return next; }
You say that the getNext function returns a pointer to Node. But, in this instance what is Node? It's not a class or a type, it's a template for a class or type. It's not complete. You need to specify the full and complete class or type:
template <typename T>
Node<T>* Node<t>::getNext(void)
{ return next; }
Note the return-type which is now a full class.
Related
This question already has answers here:
Where and why do I have to put the "template" and "typename" keywords?
(8 answers)
Closed last month.
I am really sorry if this is a duplicate post, but I am really stuck on this particular problem. For some inexplicable reason the compiler does not understand what return type Node* is on the .cpp file, here is the code:
template<typename T>
Node* BinarySearchTree<T>::DiveDownToReplace(Node* node) {
if (node->leftChild->rightChild == nullptr) {
return node->leftChild;
}
//otherwise
Node* traversingNode = node->leftChild;
Node* returnedNode;
while (true) {
if (traversingNode->rightChild->rightChild == nullptr) {
returnedNode = traversingNode->rightChild;
traversingNode->rightChild = returnedNode->leftChild;
returnedNode->leftChild = nullptr;
break;
}
traversingNode = traversingNode->rightChild;
}
return returnedNode;
}
Here is also the code in the .h(header file):
#pragma once
template<typename T>
class BinarySearchTree {
private:
struct Node
{
T data;
Node* leftChild;
Node* rightChild;
};
int m_Length = 0;
Node* root = new Node();
public:
enum class TraverseMethod
{
preorder,
inorder,
postorder,
levelorder
};
~BinarySearchTree();
void AddElement(T value);
T RemoveRoot();
bool RemoveElement(T value);
void PrintAllElements(TraverseMethod traverseMethod);
bool IsEmpty();
bool GetSize();
bool Contains(T value);
private:
void PreOrder(Node* node);
void InOrder(Node* node);
void PostOrder(Node* node);
void LevelOrder(bool deleteNode = false);
void DiveDownToAdd(T value, Node* node);
Node* DiveDownToReplace(Node* node);
};
I am getting the error "identifier Node is undefined". I tried adding BinarySearchTree::Node* instead of Node*, but I received some weird errors(c2061, syntax error: identifier 'Node'). Once more I am sorry if this post is duplicate, but coming from languages like c# and Java I am really fed up with these header issues. Thank you in advance!
There are two rather complicated technical details of C++ that get combined together here. First of all, is scoping and namespaces.
//otherwise
Node* traversingNode = node->leftChild;
This is code that's inside a member function of the BinarySearchTree template. When a symbol, such as Node gets used the compiler needs to know what in blazes is that. BinarySearchTree defines an inner class named Node, so there you go. Problem solved.
template<typename T>
Node* ...
But what the heck is this? What is this weird Node all about? This part of the C++ code is not inside a member function. You better have a global class, or something, named Node, or you'll be in big trouble.
Just because there happens to be some class or template that's defined, and it has an inner class named Node, well this means absolutely nothing, whatsoever. When some symbol name is used, in global scope, the compiler is not going to search every class for something that happens to have the same name. C++ does not work this way.
And that's why you must spell everything out:
template<typename T>
typename BinarySearchTree<T>::Node *
The "template<typename T>" stuff makes a grandiose entrance of a template parameter that's represented by symbol T, and BinarySearchTree<T>::Node spells everything out.
And the second part of this story, the only remaining question here, is what in blazes is that typename all about.
Well, that's a long story, that you can read by yourself.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
When I try to create the object of graph and pointer object of Edge it gives me segmentation fault while when I only create the graph object it does not gives me the error but when I create the graph object and Edge pointer object simultaneously it gives me the segmentation error. Why is it so? When I delete the line where the edge pointer object is declared in the main function then the code works fine but I need an edge object to display the edges...
The same code I run in any online c++ compiler gives me the correct output without any error.
#include <iostream>
using namespace std;
template <class B>
struct Edge
{
B vertex;
Edge<B> *next;
};
template <class A>
struct VertexNode
{
Edge<A> *edgeHead;
A vertex;
VertexNode *nextVertex;
};
template <class A>
class Graph
{
private:
VertexNode<A> *head;
public:
Graph();
void insertVertex(A vertex);
bool insertEdge(A vertex1, A vertex2); // this would return true if the both the vertices exists otherwise false
bool deleteVertex(A vertex); // this would return true if the vertex exists otherwise false
bool deleteEdge(A vertex1, A vertex2); // this would return true if the both the vertices exists and the edge between them exists otherwise false
bool isEmpty();
Edge<A> *Adjacent(A vertex);
};
template <class A>
Graph<A>::Graph()
{
head->edgeHead = NULL;
head->nextVertex = NULL;
head = NULL;
}
template <class A>
Edge<A> *Graph<A>::Adjacent(A vertex) // would return the list head of the edges
{
for (VertexNode<A> *temp = head; temp != NULL; temp = temp->nextVertex)
{
if (temp->vertex == vertex)
{
return temp->edgeHead;
}
}
return NULL;
}
template <class A>
bool Graph<A>::isEmpty()
{
return (head == NULL);
}
int main()
{
Graph<char> obj;
Edge<char> *edgeHead; // for displaying the list of edges
// adding vertices
obj.insertVertex('A');
obj.insertVertex('B');
obj.insertVertex('C');
obj.insertVertex('D');
obj.insertVertex('E');
obj.insertEdge('A', 'C');
// now displaying adjacent edges for checking that our graph is implemented correctly
edgeHead = obj.Adjacent('A');
}
Removing unnecessary code, we get:
template <class A>
class Graph {
VertexNode<A> *head;
Graph();
};
template <class A> Graph<A>::Graph() {
head->edgeHead = NULL;
head->nextVertex = NULL;
head = NULL;
}
At no point is memory allocated for head, so you are dereferencing an uninitialized value. That will often lead to a segmentation fault.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
#include <iostream>
using namespace std;
template <typename E>
class NodeList {
public:
class Node {
public:
Node* next;
Node* prev;
E elem;
};
public:
Node* begin() const;
NodeList();
public:
Node* header;
Node* trailer;
int size;
};
template <typename E>
NodeList<E>::NodeList(){
size = 0;
header = new Node;
trailer = new Node;
header->
trailer->
}
I want to use member variables of NodeList class, but can't use it.
such as header->next or trailer-> prev
'->' why?
I wonder why can't use it!
sorry I revised it!
from
header->trailer
to
header->next
when I type '->' then Nothing action like next, prev, elem
Well, header is a property of NodeList and is a pointer to a Node.
A Node doesn't have headers or tailers, it just has prev and next. So you can use header->next and trailer->prev if you want.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
#ifndef TEMPLATE_LINKED_LIST
#define TEMPLATE_LINKED_LIST
template <class T>
class TempSLLNODE
{
public:
T info;
TempSLLNODE *next;
TempSLLNODE( T value, TempSLLNODE *ptr = NULL)
{
info = value;
next = ptr;
}
};
template <class T>
class TempSLL
{
public:
TempSLL()
{
head = tail = 0;
}
~TempSLL();
T isEmpty()
{
return head == 0;
}
void addToHead(T);
void addToTail(T);
T deleteFromHead(); // delete the head and return its info;
T deleteFromTail(); // delete the tail and return its info;
void deleteNode(T);
bool isInList(T) const;
private:
TempSLLNODE *head, *tail;
};
#endif
TempSLLNODE is template, you need to use it with type:
update:
TempSLLNODE *head, *tail;
to:
TempSLLNODE<T> *head, *tail;
// ^^^
You could at least mention the line of the error.
For future reference, the error is here:
private:
TempSLLNODE *head, *tail;
You need to know that when you instantiate a variable from a class template, you should mention the template type. In fact, for each template type you use to instantiate a variable, the compiler compiles and generates code of the class for you, and before doing so, compiler does not generate any code for the class template.
So I guess what you meant here is:
private:
TempSLLNODE<T> *head, *tail;
I have been doing some research about this and have found a few similar questions on stackoverflow talking about the visibility of types, but this doesn't seem to be exactly the same problem (or at least that's what I think after some hours working on it).
Let's focus:
The problem
C++ compiler reports "abc.cpp:132: error: expected constructor, destructor, or type conversion before ‘*’ token"
The code where the problem is reported
template <class C, class I> ABC<C, I>::Node * ABC<C, I>::buscaTreuIRetornaMinim(Node **node) {
if (*node == NULL) return NULL;
if ((*node)->fesq != NULL) return buscaTreuIRetornaMinim(&(*node)->fesq);
Node *q = *node;
*node = *node->fdre;
return q;
}
The problem is reported on the first line, the function header. So far, I understand the problem is when specifying 'Node *' but it's already fully qualified so I don't see where's the problem.
The rest of the class definition
class ABC {
public:
ABC(void) : arrel(NULL), numelements(0), altura(0) { }
void inserir(C pclau, I pinfo);
void inordre(void);
I consultar(C pclau);
C minim(void);
C maxim(void);
void esborrar(C pclau);
private:
class Node {
public:
C clau;
I info;
Node *fesq;
Node *fdre;
Node(C pclau, I pinfo, Node *pfesq = NULL, Node *pfdre = NULL) : clau(pclau), info(pinfo), fesq(pfesq), fdre(pfdre) { }
};
Node *arrel;
Node *actual;
int numelements;
int altura;
void inserir(C pclau, I pinfo, Node **node);
void inordre(Node **node);
I consultar(C pclau, Node **node);
C minim(Node **node);
C maxim(Node **node);
void esborrar(C pclau, Node **node);
Node * buscaTreuIRetornaMinim(Node **node);
};
On the other hand, I can ensure the rest of the functions are fully functional. That's the only problem I have dealt with so far.
Any tip will be greatly appreciated. Thanks in advance for your time.
A qualified type name that includes template parameters must be prefixed with the keyword typename : typename ABC<C, I>::Node *
You can read more about about the necessity of this typename keyword here.
Looks like you need to help out the compiler recognizing Node is a type
Try this:
template <class C, class I>
typename ABC<C, I>::Node* ABC<C, I>::buscaTreuIRetornaMinim(Node **node)
{
if(*node == NULL) return NULL;
if((*node)->fesq != NULL) return buscaTreuIRetornaMinim(&(*node)->fesq);
Node *q = *node;
*node = *node->fdre;
return q;
}