C++ - Reverse function in a template class stack implementation - c++

this is my stack implementation using template with a struct type node and a class type stack:
Stack.h
#ifndef STACK_H_
#define STACK_H_
#include <cstdlib>
#include <iostream>
#include <cassert>
using namespace std;
template <class t>
struct node{
t data;
node<t>* next;
};
template <class t>
class stack
{
public:
stack();
~stack();
bool isEmpty(){ return (top_ptr=NULL);};
void push(const t&);
void pop();
t top() const;
void reverse();
void clear();
void print();
private:
node<t>* top_ptr;
};
template <class t>
stack<t>::stack()
{
top_ptr=NULL;
}
template <class t>
stack<t>::~stack()
{
while(top_ptr != NULL) pop();
}
template <class t>
void stack<t>::push(const t& source)
{
node<t>* new_node = new node<t>;
new_node->data = source;
new_node->next = top_ptr;
top_ptr = new_node;
cout << "Inserito!" << endl;
}
template <class t>
void stack<t>::pop()
{
node<t>* remove = top_ptr;
top_ptr = top_ptr->next;
delete remove;
cout << "Rimosso!" << endl;
}
template <class t>
t stack<t>::top() const
{
assert(top_ptr != NULL);
return top_ptr->data;
}
template <class t>
void stack<t>::clear()
{
node<t>* temp;
while(top_ptr != NULL)
{
temp = top_ptr;
top_ptr = top_ptr->next;
delete temp;
}
cout << "Clear completato!" << endl;
}
template <class t>
void stack<t>::reverse()
{
stack<t> new_stack;
while(top_ptr != NULL)
{
new_stack.push(top_ptr->data);
pop();
}
cout << "Reverse completato!" << endl;
}
template <class t>
void stack<t>::print()
{
node<t>* ptr = top_ptr;
while(ptr!=NULL)
{
cout << " " << ptr->data << endl;
ptr = ptr->next;
}
}
#endif /* STACK_H_ */
And this is the main.cpp:
#include "stack.h"
int main()
{
stack<int> stackino;
for(int i = 0; i<10; i++) stackino.push(i);
stackino.pop();
cout << "top(): " << stackino.top() << endl;
stackino.print();
cout << "Invoco clear()" << endl;
stackino.clear();
cout << "Stackino dopo clear():" << endl;
stackino.print();
cout << "Invoco reverse()" << endl;
stackino.reverse();
cout << "Stackino dopo reverse()" << endl;
stackino.print();
cout << "FINE!" << endl;
return 0;
}
The problem is reverse() that causes a crash of the program, I guess "top_ptr = new_stack.top_ptr" is wrong but it makes compile and execute, but crashes. Can someone help me correct this?

I think I understand the issue. Let me know if I am interpreting this wrong.
When you do top_ptr = new_stack.top_ptr, you're putting the temporary stack's top as yours.
The problem is, when that temporary stack gets destructed, it still has the same top_ptr, and deletes all the memory that was associated with it. That leaves your real stack with a bad top_ptr.
I would suggest trying:
top_ptr = new_stack.top_ptr;
new_stack.top_ptr = NULL;
So that it doesn't clear away your stack, and leave you with a bad pointer.
Hope that works?

Related

Error C2955 while implementing a struct/template class based Linked List

I am trying to implement a Linked List by a template class and a struct for defining nodes. I expected that it would compile but it's not going to. The error that Visual Studio gives to me is C2955 "use of class template requires template argument list" and checking on Microsoft documentation my code seems to not match to any reported case. The error is reported two times on line 15 of file LinkedList.cpp where the add method start. Do not look at pop function as it is not completely implemented.
LinkedList.h source code:
#pragma once
template <typename T>
struct TNode {
T info;
TNode* next;
TNode* prev;
};
template <typename T>
class LinkedList {
private:
TNode* head, * tail;
int size = 0;
public:
LinkedList();
void add(T info);
T pop();
T peekTail();
T peekHead();
void print();
void rprint();
uint64_t length();
bool empty();
void debug();
};
LinkedList.cpp source code:
#include <cstdlib>
#include <iostream>
#include "LinkedList.h"
using namespace std;
template <typename T>
LinkedList<T>::LinkedList() {
head = NULL;
tail = NULL;
}
template <typename T>
void LinkedList<T>::add(T info) {
TNode* temp = new TNode;
temp->info = info;
temp->next = NULL;
if (head == NULL) {
head = temp;
tail = temp;
}
else {
tail->next = temp;
temp->prev = tail;
tail = tail->next;
}
size++;
}
template <typename T>
T LinkedList<T>::pop() {
if (size != 0) {
T info = tail->info;
size--;
return info;
}
}
template <typename T>
T LinkedList<T>::peekTail() {
return tail->info;
}
template <typename T>
T LinkedList<T>::peekHead() {
return head->info;
}
template <typename T>
void LinkedList<T>::print() {
cout << "Elements of the Linked List: ";
TNode* temp = head;
while (temp != NULL) {
cout << temp->info << " ";
temp = temp->next;
}
}
template <typename T>
void LinkedList<T>::rprint() {
cout << "Elements of the Linked List (Reverse): ";
TNode* temp = tail;
while (temp != NULL) {
cout << temp->info << " ";
temp = temp->prev;
}
}
template <typename T>
uint64_t LinkedList<T>::length() {
return size;
}
template <typename T>
bool LinkedList<T>::empty() {
if (size == 0)
return true;
else
return false;
}
template <typename T>
void LinkedList<T>::debug() {
cout << length() << endl;
print();
cout << endl;
rprint();
}
How I do solve that problem?
You have TNode* head, * tail;, but TNode is a template class, so you must give it a type. In your case, you probably want TNode<T> *head, *tail;, so that the node contains the same as the linked list. Anywhere in your LinkedList where you use TNode would also need to specify the template parameter.
You're going to end up with other problems though. See here as for why.

Linker command failed with exit code 1 (use -v to see invocation) from my code

enter image description hereMy code that I recently build have this message :
clang: error: linker command failed with exit code 1 (use -v to see invocation)
after I build the whole project.
I have tried:
clean the build and build
I have saved my project file as workspace file and ran the file
In build settings there is no metaDataLevel so I can't flag it to None
Node.h
template<class dataType>
class Node {
private:
dataType data;
Node* next;
Node* prev;
public:
Node();
Node(dataType);
bool setNext(Node*);
bool setPrev(Node*);
Node* getNext() const;
Node* getPrev() const;
void setData(const dataType&);
dataType getData() const;
bool add(const dataType);
bool remove(const dataType);
~Node();
};
template<class dataType>
Node<dataType>::Node()
{
next = nullptr;
prev = nullptr;
}
I am not sure if this will help or not but i'll place the implementation below:
template<class dataType>
Node<dataType>::Node(dataType d)
{
data = d;
next = nullptr;
prev = nullptr;
}
template<class dataType>
bool Node<dataType>::setNext(Node* head)
{
this->next = head;
return true;
}
template<class dataType>
bool Node<dataType>::setPrev(Node* head)
{
this->prev = head;
return true;
}
template<class dataType>
Node<dataType>* Node<dataType>::getNext() const
{
return next;
}
template<class dataType>
Node<dataType>* Node<dataType>::getPrev() const
{
return prev;
}
template<class dataType>
void Node<dataType>::setData(const dataType& d){
this->data = d;
}
template<class dataType>
dataType Node<dataType>::getData() const
{
return data;
}
template<class dataType>
bool Node<dataType>::add(const dataType d){
Node* dummy = new Node(d);
dummy->setNext(this);
this->setPrev(dummy);
dummy->setPrev(nullptr);
this = dummy;
return true;
}
template<class dataType>
bool Node<dataType>::remove(const dataType d){
Node* pointer = this;
while(pointer != nullptr)
{
if(pointer->getData() == d)
{
if(pointer->getPrev() == nullptr)
{
this = pointer->getNext();
pointer = pointer->getNext();
delete pointer->getPrev();
pointer->setPrev(nullptr);
}
else if(pointer->getNext() == nullptr)
{
pointer->getPrev()->setNext(nullptr);
delete pointer;
pointer = nullptr;
}
else
{
pointer->getPrev()->setNext(pointer->getNext());
pointer->getNext()->setPrev(pointer->getPrev());
Node* dummy = pointer;
pointer = pointer->getNext();
delete dummy;
}
}
}
return true;
}
template<class dataType>
Node<dataType>::~Node(){
Node* pointer = this;
Node* holder = this->getNext();
while (holder != nullptr)
{
delete pointer;
pointer = holder;
holder = holder->getNext();
}
delete pointer;
delete holder;
}
here is the testing code i used:
int main(){
int v = 10;
string d = "X";
Node<int>* headInt = new Node<int>(v);
Node<string>* headString = new Node<string>();
headString->setData(d);
cout << "headInt contain" << headInt->getData() << ", ";
cout << "headString contain" << headString->getData() << endl << endl;
headInt->add(22);
headString->add("XXII");
cout << "headInt contain" << headInt->getData() << ", ";
cout << "headString contain" << headString->getData() << endl << endl;
headInt->remove(22);
headString->remove("XXII");
cout << "headInt contain" << headInt->getData() << ", ";
cout << "headString contain" << headString->getData() << endl << endl;
}
I am expecting to have this as my result :
headInt contain 10,
headString contain X
headInt contain 22,
headString contain XXII
headInt contain 10,
headString contain X

nullptr = Node is not assigning correctly

please don't dig into me too hard, I am still steadily learning and ran into an issue when trying to construct an AVL tree. When iterating through the tree on insert, I go until I reach a nullptr, create a new node, and assign that ptr to the nullptr. The value is never accepted though. Can someone find the error and explain it to me? ty!
#ifndef AVLTree_hpp
#define AVLTree_hpp
#include <stdio.h>
#include <stack>
template<typename T>
class AVLTree{
private:
struct Node{
T val;
Node* left;
Node* right;
int height;
Node(T V)
:left{nullptr},right{nullptr}
{
val = V;
}
~Node(){
}
};
Node* head;
void rightRotate(Node*& node);
void leftRotate(Node*& node);
void leftRight(Node*& node);
void rightLeft(Node*& node);
public:
AVLTree();
~AVLTree();
AVLTree(const AVLTree &c);
AVLTree(AVLTree &&c);
AVLTree &operator=(const AVLTree &c);
AVLTree &operator=(AVLTree &&c);
void add(T value);
int getHeight(Node* n);
};
template <typename T>
AVLTree<T>::AVLTree()
:head{nullptr}{
}
template <typename T>
AVLTree<T>::~AVLTree(){
}
template <typename T>
void AVLTree<T>::rightRotate(Node*& node){
Node* temp = node;
node = node->left;
Node* leftLL = node->right;
temp->left = leftLL;
node->right = temp;
}
template <typename T>
void AVLTree<T>::leftRotate(Node*& node) {
Node* temp = node;
node = node->right;
Node* yL = node->left;
temp->right = yL;
node->left = temp;
}
//left right condition
template <typename T>
void AVLTree<T>::leftRight(Node*& node) {
leftRotate(node->left);
rightRotate(node);
}
//right left condition
template <typename T>
void AVLTree<T>::rightLeft(Node*& node){
rightRotate(node->right);
leftRotate(node);
}
template <typename T>
void AVLTree<T>::add(T value){
if(head==nullptr){
head = new Node(value);
return;
}
std::stack<Node*> st;
Node* it = head;
while(it!=nullptr){
st.push(it);
if(value <= it->val){
it = it->left;
}else{
it=it->right;
}
}
//here is where the it is not assigned to the new node pointer.
//I have tested it and the node is created, "it" just does not hold the value at any point.
it = new Node(value);
int count = 0;
while(!st.empty()){
int balance = getHeight(st.top()->left) - getHeight(st.top()->right);
if(balance > 1){
if(st.top()->left!= nullptr&&st.top()->left!=nullptr){
leftRotate(st.top());
}else{
leftRight(st.top());
}
}else if(balance<-1){
if(st.top()->right!=nullptr&&st.top()->right!=nullptr){
rightRotate(st.top());
}else{
rightLeft(st.top());
}
}
st.pop();
if(++count==4){
break;
}
}
}
template <typename T>
int AVLTree<T>::getHeight(Node* n){
int max =0;
if(n!=nullptr){
max = std::max(getHeight(n->left),getHeight(n->right))+1;
}
return max;
}
#endif /* AVLTree_hpp */
It is a copy of the pointer and updating it has no effect on the original pointer. You need to do something like this instead:
Node* it = head;
bool left = true;
while(it!=nullptr){
st.push(it);
left = value <= it->val;
if(left){
it = it->left;
}else{
it=it->right;
}
}
it = new Node(value);
if (left){
stack.top()->left = it;
} else {
stack.top()->right = it;
}
Consider this simplified version of your code:
#include <iostream>
struct Linked {
Linked* next;
};
int main(int argc, char** argv) {
Linked l0 {nullptr};
// Case 1: Does not work
std::cout << "case 1" << std::endl;
Linked* node = l0.next;
node = new Linked {nullptr};
std::cout << "node=" << std::hex << node << std::endl;
std::cout << "l0.next=" << std::hex << l0.next << std::endl;
free(node);
std::cout << std::endl;
// Case 2: Works
std::cout << "case 2" << std::endl;
l0.next = new Linked {nullptr};
std::cout << "l0.next=" << std::hex << l0.next << std::endl;
free(l0.next);
l0.next = nullptr;
std::cout << std::endl;
// Case 3: Works
std::cout << "case 3" << std::endl;
Linked** nodeP = &(l0.next);
*nodeP = new Linked {nullptr};
std::cout << "*nodeP=" << std::hex << *nodeP << std::endl;
std::cout << "l0.next=" << std::hex << l0.next << std::endl;
free(l0.next);
l0.next = nullptr;
}
Which outputs:
$ ./main
case 1
node=0x7fba0d400620
l0.next=0x0
case 2
l0.next=0x7fba0d400620
case 3
*nodeP=0x7fba0d400620
l0.next=0x7fba0d400620
Case 1: does not work because the new Node is assigned to a copy of the left/right child pointer (i.e. not the actual child node from the parent node)
Case 2: works as expected because the new node is assigned directly to one of the parent's child node.
Case 3: also works because instead of assigning the new node to a copy of the child pointer, you assign it to a pointer referencing the pointer to child itself. In this respect, case 2 and 3 are equivalent.

c++ List class as Student type

I am trying to use a class Student and declare it as a list type. I can pushback but without changing the List.h or Node.h how can I print the data in list2? The given print() function within List..h does not work :(
Node.h
#ifndef NODE_H
#define NODE_H
#include <string>
#include <iostream>
using namespace std;
template <typename T>
class Node {
private:
T data;
Node<T>* next;
public:
Node(T);
virtual ~Node(); // base class destructor must be virtual
template <typename U> friend class List;
};
template <typename T>
Node<T>::Node(T d) {
data = d;
next = NULL;
}
template <typename T>
Node<T>::~Node() {
}
#endif /* STRNODE_H */
List.h
#ifndef LIST_H
#define LIST_H
#include "Node.h"
// Singly linked list
template <typename T>
class List {
private:
Node<T>* head; // pointer to the first node
Node<T>* tail; // pointer to the last node
int count; // number of nodes in the list
public:
class OutOfRangeException{ }; // empty inner class for exception handling
List();
virtual ~List();
void push_back(T item);
void insert(int index, T item);
void remove(int index);
int indexOf(T item);
T get(int position); // OutOfRangeException is generated
bool isEmpty();
int size();
void print();
};
template <typename T>
List<T>::List() {
head = tail = NULL;
count = 0;
}
template <typename T>
List<T>::~List() {
Node<T>* discard;
while (head != 0) {
discard = head;
head = head->next;
delete discard;
}
}
// append an item at the end of the StrList
template <typename T>
void List<T>::push_back(T item) {
try {
Node<T>* newNode = new Node<T>(item);
if (head == 0) {
head = tail = newNode;
} else {
tail->next = newNode;
tail = newNode;
}
++count;
} catch (bad_alloc &e) {
cout << "memory allocation exception: " << e.what() << endl;
exit(1);
}
}
// insert an item at the specified index
template <typename T>
void List<T>::insert(int index, T item) {
try {
if (index < 0 || index > count) // push_back() if index == count
throw OutOfRangeException();
Node<T>* newNode = new Node<T>(item);
if (head == 0) { // empty
head = tail = newNode;
} else if (index == 0) { // at the start
newNode->next = head;
head = newNode;
} else if (index == count) { // at the end
tail->next = newNode;
tail = newNode;
} else { // insert in the middle
Node<T>* prevNode;
Node<T>* currNode = head;
for (int i = 0; i < index; i++) {
prevNode = currNode;
currNode = currNode->next;
}
// insert between 'prevNode' and 'currNode'
prevNode->next = newNode;
newNode->next = currNode;
}
++count;
} catch (bad_alloc &e) {
cout << "memory allocation exception: " << e.what() << endl;
exit(1);
}
}
// is the StrList empty?
template <typename T>
bool List<T>::isEmpty() {
return count == 0;
}
// remove the item at specified index
template <typename T>
void List<T>::remove(int index) {
if (index < 0 || index >= count)
throw OutOfRangeException();
if (index == 0) { // at the start
Node<T>* discard = head;
head = head->next;
delete discard;
} else {
Node<T>* prevNode;
Node<T>* currNode = head;
for (int i = 0; i < index; i++) {
prevNode = currNode;
currNode = currNode->next;
}
// remove 'currNode'
prevNode->next = currNode->next; // bypass
delete currNode;
if (index == count - 1) // last node was removed. Update 'tail'
tail = prevNode;
}
--count;
if (count == 0)
tail = NULL;
}
// retrieve the item at the given position of the StrList. position starts from 0.
// throws OutOfRangeException if invalid position value is given.
template <typename T>
T List<T>::get(int position) {
if (position < 0 || position >= count)
throw OutOfRangeException();
int loc = 0;
Node<T>* curr = head;
while (loc < position) {
++loc;
curr = curr->next;
}
return curr->data;
}
// Requirement:
// != operator of <class T> is used
template <typename T>
int List<T>::indexOf(T item) {
if (head == 0) {
return -1; // not found
} else {
int index = 0;
Node<T>* currNode = head;
while (currNode->data != item && currNode != NULL) {
currNode = currNode->next;
++index;
}
if (currNode == NULL) // not found thru the end
return -1;
else
return index;
}
}
// number of nodes in the StrList
template <typename T>
int List<T>::size() {
return count;
}
// Requirement:
// << operator for <class T> is used.
template <typename T>
void List<T>::print() {
cout << "*** StrList contents ***" << endl;
for (int i = 0; i < count; i++) {
cout << i << ": " << get(i) << endl;
}
}
#endif
Student.h
#include "List.h"
class Student {
private:
string name;
int id;
public:
Student();
Student(string a);
virtual ~Student();
friend ostream& operator<<(ostream &os, const Student& p);
bool operator!=(const Student &p) const;
bool operator==(const Student &p) const;
};
Student::Student() {
}
Student::Student(string a) {
name = a;
}
Student::~Student() {
}
ostream& operator<<(ostream &os, const Student& p) {
return os << p.name;
}
bool Student::operator==(const Student &p) const {
// Compare the values, and return a bool result.
if (name == p.name)
return true;
else
return false;
}
bool Student::operator!=(const Student &p) const {
return !(*this == p);
}
main.cpp
#include <iostream>
using namespace std;
#include "Student.h"
int main() {
cout << "\n*** StrList Test ***" << endl;
List<string> list;
list.push_back("zero");
list.push_back("one");
list.push_back("two");
list.push_back("three");
list.push_back("four");
list.push_back("five");
list.print();
list.insert(1, "inserted at position 1");
list.insert(0, "inserted at position 0");
list.insert(4, "inserted at position 4");
list.print();
cout << "removing at indexes 3, 0" << endl;
list.remove(3);
list.remove(0);
list.print();
list.insert(2, "inserted at position 2");
list.print();
cout << "five is at index " << list.indexOf("five") << endl;
cout << "two is at index " << list.indexOf("two") << endl;
//Test for my Student class implementation
// Student<string> st1; //Create new student Ryan Martin with id of 1
List<Student> list2;
Student stu("Ryan Martin");
list2.push_back(stu);
//list2.print();
//list2.push_back("Ryan");
//list2.PrintStudents(); //Test that the Student class successfully stored and can access
return 0;
}
The << operator must be defined for your Student class. To quote List.h:
// Requirement:
// << operator for <class T> is used.
template <typename T>
void List<T>::print() {
cout << "*** StrList contents ***" << endl;
for (int i = 0; i < count; i++) {
cout << i << ": " << get(i) << endl;
}
}
So in your Student class, you need to implement the operator<<(ostream &out);
Do it as a friend (friends are fun!):
friend std::ostream& operator<< (std::ostream &out, const Student &stu)
{
return out << stu.name << " id: " << stu.id << std::endl;
}
Here is a good reference:
http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/
If I understood you correctly, then you want to define operator<< for your student class, which you can do for example like this:
friend std::ostream & operator<<(std::ostream & os, const Student & s)
{
return os << s.name << " " << s.id << std::endl;
}
Note however that I have not tested this code and I haven't read all the snippets you posted,
so I might have understood you wrongly.
EDIT:
So after trying it out with Visual Studio, the full version of you student class should be like this:
#include "List.h"
class Student {
private:
string name;
int id;
public:
Student();
Student(string a);
virtual ~Student();
friend std::ostream & operator<<(std::ostream & os, const Student & s)
{
return os << s.name << " " << s.id << std::endl;
}
};
Student::Student() {
}
Student::Student(string a) {
name = a;
}
Student::~Student() {
}
Also not that you do not have to make the destructor in Student virtual unless you plan on having it as a base class for other classes.
the print function require an operator << to be defined on your student class and it's not the case
so define how a student will be display by << and it should work!

Using Template Defined Variables in Function Calls in C++

I have a function that I am passing in a variable for; the variable, however, is a template variable. I do not know the specifics on how I am supposed to create a function with a template parameter. I do not even know if I am asking the correct question, but when I compile the program, I get the following error in the main.cpp file:
line 17 error: no matching function for call to 'BSTree<int>::BSTinsert(TNode<int>&)'
line 49 error note: candidates are: void BSTree<T>::BSTinsert(const T&) [with T = int]
I thought that I was doing everything right, but this is the one error that has hindered me from moving on. Any and all help is welcome and appreciated! (I don't think that the other two files -- treeNode.h and treeNode.cpp -- need to be shown, but I have been wrong before. If they are, lemme know and I will gladly post them. Thank you once again!
main.cpp:
#include <iostream>
#include <string>
#include "BSTree.cpp"
using namespace std;
int main()
{
BSTree<int> bt;
TNode<int> item;
for(int i=0; i<13; i++)
{
cout << "Enter value of item: ";
cin >> item;
bt.BSTinsert(item); //this is the line with the error
}
cout << "Hello world!" << endl;
return 0;
}
BSTree.h:
#ifndef BSTREE_H_INCLUDED
#define BSTREE_H_INCLUDED
#include "treeNode.cpp"
template <typename T>
class BSTree
{
TNode<T> *root;
void insert(TNode<T> * & r, const T & item);
public:
BSTree();
TNode<T>* getRoot();
void BSTinsert(const T & item);
};
#endif // BSTREE_H_INCLUDED
BSTree.cpp:
#include <iostream>
#include <string>
#include "BSTree.h"
template <typename T>
void BSTree<T>::insert(TNode<T> * & r, const T & item)
{
if(r == NULL)
TNode<T> newNode = new TNode<T>(item);
else if(r == item)
return;
else if(r->nodeValue > item)
insert(r->leftChild, item);
else if(r->nodeValue > item && r->leftChild == NULL)
{
TNode<T> newNode = new TNode<T>(item);
r->leftChild = newNode;
newNode->parent = r;
}
else if(r->nodeValue < item)
insert(r->rightChild, item);
else if(r->nodeValue < item && r->rightChild == NULL)
{
TNode<T> newNode = new TNode<T>(item);
r->rightChild = newNode;
newNode->parent = r;
}
}
template <typename T>
BSTree<T>::BSTree()
{
root = NULL;
}
template <typename T>
TNode<T>* BSTree<T>::getRoot()
{
return root;
}
template <typename T>
void BSTree<T>::BSTinsert(const T& item) //this is the line the note is referring to
{
TNode<T> tempRoot = getRoot();
insert(tempRoot, item);
}
treeNode.cpp:
#include <string>
#include <iostream>
#include "treeNode.h"
using namespace std;
template <typename T>
TNode<T>::TNode()
{
parent = NULL;
leftChild = NULL;
rightChild = NULL;
nodeValue = 0;
}
template <typename T>
TNode<T>::TNode(const T&item, TNode<T> *left, TNode<T> *right, TNode<T> *par)
{
parent = par;
leftChild = left;
rightChild = right;
nodeValue = item;
}
template <typename T>
void TNode<T>::printNodeInfo()
{
cout << "Value: " << nodeValue << endl;
if(parent != NULL)
cout << "Parent Value: " << parent << endl;
if(leftChild != NULL)
cout << "Left Child Value: " << leftChild << endl;
if(rightChild != NULL)
cout << "Right Child Value: " << rightChild << endl;
}
treeNode.h:
#ifndef TREENODE_H_INCLUDED
#define TREENODE_H_INCLUDED
template <typename T>
class TNode
{
public:
T nodeValue;
TNode<T> *leftChild, *rightChild, *parent;
TNode();
TNode(const T&item, TNode<T> *left = NULL, TNode<T> *right = NULL, TNode<T> *par = NULL);
void printNodeInfo();
friend std::istream& operator>>( std::istream& i, TNode<T>& item ){return i;}
};
#endif // TREENODE_H_INCLUDED
Does this solve your problem?
int main()
{
BSTree<int> bt;
int item;
for(int i=0; i<13; i++)
{
cout << "Enter value of item: ";
cin >> item;
bt.BSTinsert(item); //this is the line with the error
}
cout << "Hello world!" << endl;
return 0;
}