Here is a simple c++ class for binary tree. Compiler throws an error:
E0147 declaration is incompatible with "void BinaryTree::getLeftChild(node *n)"
Here node is a struct defined under the private section in the class. I am not sure why it says incompatible declaration.
//------------------------ BinaryTree class-----------------
class BinaryTree
{
public:
BinaryTree();
~BinaryTree();
void createRootNode();
void getChildren();
void getLeftChild(node* n);
void getRightChild(node* n);
private:
typedef struct node
{
node *lchild = nullptr;
int data;
node *rchild = nullptr;
}node;
queue <node*> Q;
node *root;
};
BinaryTree::BinaryTree()
{
createRootNode();
getChildren();
}
void BinaryTree::createRootNode()
{
root = new node();
cout << "Enter value for root node" << endl;
cin >> root->data;
Q.push(root);
}
void BinaryTree::getChildren()
{
while (Q.empty == false)
{
getLeftChild(Q.front());
getRightChild(Q.front());
Q.pop();
}
}
void BinaryTree::getLeftChild(node* n)
{
}
void BinaryTree::getRightChild(node* n)
{
}
Code picture with errors
I got another struct in global scope declared as "node" which created chaos. Secondly, i also need to fix the order of public and private sections.
Here is working code
//------------------------ BinaryTree class-----------------
class BinaryTree
{
private:
typedef struct node
{
node *lchild = nullptr;
int data;
node *rchild = nullptr;
}node;
queue <node*> Q;
node *root;
public:
BinaryTree();
~BinaryTree();
void createRootNode();
void getChildren();
void getLeftChild(node* n);
void getRightChild(node* n);
};
BinaryTree::BinaryTree()
{
createRootNode();
getChildren();
}
void BinaryTree::createRootNode()
{
root = new node();
cout << "Enter value for root node" << endl;
cin >> root->data;
Q.push(root);
}
void BinaryTree::getChildren()
{
while (Q.empty() == false)
{
getLeftChild(Q.front());
getRightChild(Q.front());
Q.pop();
}
}
void BinaryTree::getLeftChild(node* n)
{
}
void BinaryTree::getRightChild(node* n)
{
}
First error, is that you need to forward declare the node.
Second error, is that you are trying to access node which is privately declared inside of BinaryTree.
First answer:
typedef struct node
{
node* lchild = nullptr;
int data;
node* rchild = nullptr;
}node;
class BinaryTree
{
public:
BinaryTree();
~BinaryTree();
void createRootNode();
void getChildren();
void getLeftChild(node* n);
void getRightChild(node* n);
private:
node* root;
};
void BinaryTree::getLeftChild(node* n)
{
}
void BinaryTree::getRightChild(node* n)
{
}
Now code compiles fine.
Or if you want to have the typedef defined as private inside, you need the implementation to be inside the class as well.
Second Answer:
typedef struct node;
class BinaryTree
{
public:
BinaryTree();
~BinaryTree();
void createRootNode();
void getChildren();
void getLeftChild(node* n)
{
}
void getRightChild(node* n)
{
}
private:
typedef struct node
{
node* lchild = nullptr;
int data;
node* rchild = nullptr;
}node;
node* root;
};
Related
I'm writing a function which iterates a Queue from within a queue class which operates off of a LinkedList/Node data structure.
I've been able to make the function work but only by getting a pointer to the head node directly from the LinkedList class which, as I understand it, is considered poor encapsulation.
This is my code:
main():
int main()
{
Queue list;
int nums[] = {60, 50, 40};
for (int i=0; i<(int)sizeof(nums)/(int)sizeof(nums[0]); i++) {list.enqueue(nums[i]);}
list.iterate();
}
Queue:
.h
#include "LinkedList.h"
class Queue
{
public:
typedef int value_type;
Queue();
void enqueue(value_type& obj);
int size() const;
void iterate();
int min();
private:
LinkedList data;
int used;
};
#include "Queue.hpp"
.hpp
Queue::Queue()
{ data = LinkedList(); used = 0; }
void Queue::enqueue(value_type& obj)
{ ++used; data.addToTail(obj); }
int Queue::size() const
{ return used; }
void Queue::iterate()
{
node * temp = data.get_head();
for (int i = 0; i < size(); i++)
{ cout << temp->get_data() << endl; temp = temp->get_next(); }
delete temp;
}
LinkedList
.h
#include "Node.h"
class LinkedList
{
public:
typedef int value_type;
LinkedList();
void addToHead(typename node::value_type& entry);
void addToTail(typename node::value_type& entry);
node * get_head();
int front();
private:
node* head;
node* tail;
node* current;
};
#include "LinkedList.hpp"
.hpp
LinkedList::LinkedList()
{ head = NULL; tail = NULL; current = NULL; }
void LinkedList::addToTail(value_type& entry)
{
if (get_head() == NULL)
{ addToHead(entry); }
else {
node* add_ptr = new node;
add_ptr->set_data(entry);
add_ptr->set_next(current->get_next());
add_ptr->set_previous(current);
current->set_next(add_ptr);
if (current == tail) {tail = current->get_next();}
current = current->get_next();
}
}
void LinkedList::addToHead(value_type& entry)
{ head = new node(entry, head); if (tail == NULL) {tail = head;} current = head; }
node * LinkedList::get_head()
{ return head; }
int LinkedList::front()
{ int rval = head->get_data();return rval; }
Node
.h
class node
{
public:
typedef int value_type;
node();
node(const value_type& data, node* link);
void set_data(const value_type& new_data);
void set_next(node* next_ptr);
void set_previous(node* last_ptr);
int get_data() const;
node* get_next() const;
node* get_previous() const;
private:
value_type data;
node* next;
node* previous;
};
#include "Node.hpp"
.hpp
node::node()
{ data = 0; next = 0; previous = 0; }
node::node(const value_type& data, node* link)
{ this->data = data; this->next = link; this->previous = NULL; }
void node::set_data(const value_type& new_data) {data = new_data;}
void node::set_next(node* next_ptr) {next = next_ptr;}
void node::set_previous(node* last_ptr) {previous = last_ptr;}
int node::get_data() const {return data;}
node* node::get_next() const {return next;}
node* node::get_previous() const {return previous;}
Is it possible to iterate the LinkedList without directly retrieving a pointer node? And is this bad practice?
You do not expose the (internal) data structures of the linked list within the interface of the Queue-class (i.e. in the header file). You're just using these data structures in the implementation. Hence, I'd say that you do not "violate encapsulation".
But of course, you may adapt the interface of your LinkedList, such that it does not make use of the internal data structures directly. The standard library with its iterators shows how such a concept is realized. An iterator is an object that represents the position of an element in the container, (and it offers access to the respective element).
The encapsulation in Queue isn't violated but in LinkedList it is, you shouldn't have get_head() function that returns a private pointer member (what if someone does something like this: list.get_head()->set_next(NULL)). You need to create an iterate function in LinkedList and than Queue::iterate would just call this function.
I'm new to C++. I'm trying to implement a LinkedList, for which I created two classes Node and LinkedList.
I created some test functions. One to test the Node creation and another to test the isEmpty function from LinkedList. However, when I try to test them. What's created in 'testNode()ends up being in the same Node I create insideLinkedListashead`.
This may be trivial question, however as a newcomer to C++ this concept is still no clear to me. I'd like to know why it is referring to the same instance created previously.
#include <iostream>
#include <assert.h>
using namespace std;
class Node
{
private:
int data;
int next;
public:
int getData(){return data;}
void setData(int new_data) {data = new_data;}
int getNext(){return next;}
void setNext(int new_next) {next = new_next;}
};
class LinkedList
{
Node head;
Node head2;
public:
bool isEmpty()
{
if (head.getData() == 0) {return true;}
return false;
}
};
void testNode()
{
Node aNode;
aNode.setData(15);
aNode.setNext(23);
assert (aNode.getData() == 15);
assert (aNode.getNext() == 23);
}
void testEmptyLinkedList()
{
LinkedList ll;
assert (ll.isEmpty() == true);
}
Initialize your data.
int data = 0;
int next = 0;
Live On Coliru
#include <iostream>
#include <cassert>
using namespace std;
class Node {
private:
int data = 0;
int next = 0;
public:
int getData() { return data; }
void setData(int new_data) { data = new_data; }
int getNext() { return next; }
void setNext(int new_next) { next = new_next; }
};
class LinkedList {
Node head;
Node head2;
public:
bool isEmpty() {
if (head.getData() == 0) {
return true;
}
return false;
}
};
void testNode() {
Node aNode;
aNode.setData(15);
aNode.setNext(23);
assert(aNode.getData() == 15);
assert(aNode.getNext() == 23);
}
void testEmptyLinkedList() {
LinkedList ll;
assert(ll.isEmpty() == true);
}
int main() {
testEmptyLinkedList();
}
If your intention is to implement LinkList, each node of the list should contain the address of the next one.
So "next" shoud be declared as a pointer to a Node. Same for the first node of the list.
class Node {
private:
int data;
Node *next;
....
};
class LinkedList {
private:
Node *head;
...
};
I have a header file containing this class definition:
class visitorlist {
struct Node {
visitor vis;
Node* next;
};
Node* head;
Node* tail;
public:
visitorlist() { //written here to have it as inline.
head = NULL;
tail= NULL;
}
~visitorlist();
int lengthvl();
void add(const visitor);
void popandexit();
void transfer(visitorlist);
void deletenode(Node*);
int refiprio();
int refioffno();
int refifloor();
visitor reravi();
bool isempty();
Node* rehead();
};
and in a source file with the above header included I have:
Node* visitorlist::rehead() {
return head;
}
This causes an error: 'Node' does not name a type.
Isn't Node on the scope of the function?
Use
visitorlist::Node* visitorlist::rehead() {
return head;
}
Or, since C++11:
auto visitorlist::rehead() -> Node* {
return head;
}
I'm confused about how pointers work here. I have a class called PrefixTree and a struct called TreeNode within this class. I have the following code to build a tree with a string, the problem is every time preorder() is called it doesn't return the root character correctly and after that it throws a segmentation fault.
I want to know if I'm correctly setting the root pointer or if I'm misusing it in the others files.
//prefixtree.cpp
PrefixTree::TreeNode* PrefixTree::buildTree(string& input)
{
char c = input[0];
input.erase(0,1);
TreeNode* node = new TreeNode();
node->character = c;
if (!root)
root = node;
if (c == '*')
{
node->left = buildTree(input);
node->right = buildTree(input);
}
return node;
}
void PrefixTree::preorder()
{
traverse(root);
}
void PrefixTree::traverse(TreeNode* node)
{
if (node)
{
cout << node->character << endl;
traverse(node->left);
traverse(node->right);
}
}
and
//prefixtree.h
class PrefixTree
{
private:
struct TreeNode
{
char character;
TreeNode* left;
TreeNode* right;
};
TreeNode* root;
void traverse(TreeNode* node);
public:
TreeNode* buildTree(string& input);
void preorder();
};
and
//main.cpp
PrefixTree tree;
string a = string("*a**!*dc*rb");
cout << tree.buildTree(a)->character << endl;
tree.preorder();
You need to initialize the pointer members, they are not automatically initialized and may contain garbage values instead of nullptr, which leads to your tests if(!root) and if(node) being useless, reporting uninitialized pointers as valid, which you will then dereference.
Add constructors:
class PrefixTree
{
private:
struct TreeNode
{
char character;
TreeNode* left;
TreeNode* right;
TreeNode() : character('a'), left(nullptr), right(nullptr) {};
};
TreeNode* root;
void traverse(TreeNode* node);
public:
PrefixTree() : root(nullptr) {};
TreeNode* buildTree(string& input);
void preorder();
};
Just started learning c++ for a class, I can't figure out what is wrong with this code! I'm making a stack class with a helper class nested inside it called node that acts as a linked list. The error I'm getting is on line 12 and is:
Stack.cpp: In destructor ‘Stack::~Stack()’:
Stack.cpp:12:24: error: request for member ‘getNext’ in ‘((Stack*)this)->Stack::node’, which is of non-class type ‘Stack::Node*’
Here's my code:
#include "Stack.h"
Stack:: Stack ()
{
height = 0;
node = 0;
}
Stack:: ~Stack()
{
while(node != 0){
Node *next = *node.getNext();
delete node;
node = next;
}
node = 0;
}
And Here's my header file:
using namespace std;
class Stack
{
private:
int height;
class Node{
private:
int data;
Node* next;
public:
void setData(int x){
data = x;
}
void setNext(Node* x){
next = x;
}
int getData(){
return data;
}
Node* getNext(){
return next;
}
};
Node* node;
public:
Stack();
~Stack();
void push(int x);
int pop();
int peek();
int getHeight();
bool isEmpty();
};
Node *next = *node.getNext();
should be
Node *next = (*node).getNext();
Since . operator has higher precedence than * deference operator.
You can also use:
Node *next = node->getNext();