Why can't use '->' c++ template class [closed] - c++

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.

Related

Identifier Node* is undefined(linker problem) [duplicate]

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.

Declare methods with pointer outside class in C++ [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I want to declare the methods of the following code outside the class but i get the following error whenever the method is a pointer to private member variable:
"no instance of function template "std::next" matches the required typeC/C++(386)".
class Node
{
private:
int data;
Node *next;
public:
Node() {}
int GetData() { return data; }
Node *GetNext() { return next; }
void SetData(int aData) { data = aData; }
void SetNext(Node *aNext) { next = aNext; }
};
// outside try class declaration of
int Node::GetData() { return data; }
Node Node::*GetNext() { return next; } // here is the error!!
Would you help me?
This is wrong
Node Node::*GetNext() { return next; }
This is right
Node* Node::GetNext() { return next; }
The name of function is Node::GetNext and not GetNext.
You must put the asterisk after the return type because the return type is a pointer to Node object
like this:
Node *Node::GetNext() { return next; }
When you write Node *GetNext();, this means the method name is GetNext and the return type is Node *. It doesn't matter whether you put the asterisk near the method name or away from it.
Outside of the class, you need fully qualified name of the method which is Node::GetNext with return type Node *. So it would look like Node *Node::GetNext(); or Node* Node::GetNext(); depending on your style of the placement of the asterisk.

segmentation fault in an sorted linked list program for Homework [closed]

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 3 years ago.
Improve this question
i know there are already so many question and answer about this error. But with many Topic i still can not find the Problem in my Program. I must write a sorted Linked List, but i tried and tried and got the Error when i´m trying to get the next Node. I am pretty new to C++ so can u guys please help me out . And sorry for my bad English.
Booking.h
class Booking {
public:
Booking() : data{nullptr},nextNode{nullptr}, prevNode{nullptr}
{}
Booking(Booking* d) :data{d}, nextNode{nullptr}, prevNode{nullptr} {}
Booking* getNextNode() const {
return nextNode; // where the SIGSEGV happened, i checked by debugger
}
private:
Booking* nextNode;
Booking* prevNode;
Booking* data;
}
SortedLinkedList.h
template<typename T>
class SortedLinkedList {
public:
SortedLinkedList() {
root = NULL;
end = new T;
cursor = end;
size = 0;
}
void insertNode(T* data)
{
T* node = new T(data);
cursor = root;
while(cursor->getNextNode()){ // here it go to getNextNode
}
}
private:
T* root;
T* cursor;
T* end; }
TravelAgency.h
class TravelAgency{
public:
void readFile();
private:
SortedLinkedList<Booking> allBookings ;
}
TravelAgency.cpp
void TravelAgency::readFile(){
allBookings.insertNode(flight); // flight is an obj of an derived class
}
In the constructor, you set root to Null.
Then in insertNode, you do:
cursor = root;
while(cursor->getNextNode())
You need to allocate a root node, or deal with it being Null.

what means "pointer was not declared in this scope" [closed]

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.

error 'TempSLLNODE' : use of class template requires template argument list [closed]

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;