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.
Related
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.
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 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 6 years ago.
Improve this question
Basically, I'm getting problems in both the header file and the cpp file with the search function. It just says "Member declaration not found" and "prototype for 'node *BTree::search(int) does not match any in class BTree". I only listed the search functions in the cpp file to make it easier, because my insert and destroy_tree functions both work fine.
Header file:
#ifndef BTREE_H_
#define BTREE_H_
class BTree {
public:
struct node{
int key_value;
node *left;
node *right;
};
BTree();
virtual ~BTree();
void insert(int key);
node* search(int key);
void destroy_tree();
node *root;
private:
void insert(int key, node *leaf);
node* search(int key, node *leaf);
void destroy_tree(node *leaf);
};
#endif /* BTREE_H_ */
Implementation:
#include "BTree.h"
#include <iostream>
using namespace std;
struct node{
int key_value;
node *left;
node *right;
};
BTree::BTree() {
root = NULL;
}
BTree::~BTree() {
destroy_tree();
}
node BTree::*search(int key, node *leaf){
if(leaf != NULL){
if(key == leaf->key_value){
return leaf;
}
if(key < leaf->key_value){
return search(key, leaf->left);
}
else{
return search(key, leaf->right);
}
}
else return NULL;
}
node *BTree::search(int key){
return search(key, root);
}
You have two struct node structs, one declared globally (in your .cpp file) and one declared within the BTree class. These are two different structures, one named ::node (global), the other BTree::node. In your header file search refers to the one defined within the class, while the function declaration in the .cpp file refers to the global one.
Remove the global struct, and declare the search function using BTree::node *BTree::search instead.
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 8 years ago.
Improve this question
My program is compiling but I'm getting a seg fault when I attempt to run this code. What I'm trying to do is append an element to the end of a linked list. Here is what my application is doing:
int main()
{
linklist<int> l;
int i = 30;
l.insertEnd(i);
return (0);
}
And here is the implementation of the function from my class:
template <class T>
void linklist<T>::insertEnd(T anItem)
{
if(this->headPointer = NULL)
{
headPointer = new node(anItem, NULL);
}
else
{
node* endPointer = headPointer;
while(endPointer->linkPointer != NULL)
{
endPointer = endPointer->linkPointer;
}
endPointer->linkPointer = new node(anItem, NULL);
}
};
Lastly, here is how my node is set up:
class node
{
public:
T dataItem;
node* linkPointer;
// construct a new node and initialize its attributes with the given parameters.
node(T i, node* l): dataItem(i), linkPointer(l)
{
};
};
node* headPointer;
};
template <class T>
void linklist<T>::insertEnd(T anItem)
{
if(this->headPointer == NULL) //you were assigning null instead of comparing
{
headPointer = new node(anItem, NULL);
}
//rest of the code here
Try this
It seems like issue in this statement, here instead of comparing you are assigning.
if(this->headPointer = NULL)
Use this:
if(this->headPointer == NULL)
or
if(NULL == this->headPointer) // This is better way to compare.
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;