Class Linked List Template run time error - c++

I have a linked list class that uses templates for both the Node class and the List class. I've done some debugging and come to the conclusion that in my Node class, the member functions cannot access the member data, but the constructors can. I would like to know how i can fix this!
#include <iostream>
#include <fstream>
#include <string>
#include "List.h"
using namespace std;
int main()
{
ifstream fileIn("data1.txt");
List<int> studentList;
if(fileIn.fail())
cout << "file did not open" << endl;
else
studentList.add(fileIn);
fileIn.close();
cin.get();
cin.ignore();
return 0;
}
//List.h
//ignore the commented methods, have yet to write them.
#ifndef LIST_H
#define LIST_H
#include <cstdlib>
#include <iostream>
#include <fstream>
#include "Node.h"
using namespace std;
template <class NumType>
class List
{
int counter;
bool isEmpty();
const bool print(){
}
public:
Node<NumType> * head;
List()
{
this->head = NULL;
counter = 0;
}
~List()
{
}
//place in an order thart keeps the array in ascending order
void add(ifstream &);
/*
Node* location(Node *){
}
bool remove(const int){
}
bool clear(){
}
const void peek(const Node*){
}
//average of all test scores or just one students test scores?
float average(){
}
char grade(){
}
*/
};
#include "List.cpp"
#endif
//List.cpp
#include "List.h"
using namespace std;
template <class NumType>
bool List<NumType> :: isEmpty()
{
cout << "inside isEmpty" << endl;
return(head == NULL);
}
template <class NumType>
void List<NumType> :: add(ifstream & fin)
{ int dummyID;
NumType tests[3];
string dummyName;
while(fin >> dummyID)
{ fin.ignore();
getline(fin, dummyName);
for(int x = 0; x < 3; x++)
fin >> tests[x];
fin.ignore();
cout << dummyID << endl;
cout <<dummyName << endl;
for(int y = 0; y < 3; y++)
cout << tests[y] << " ";
if(isEmpty())
{
this->head = new Node<NumType>(NULL, tests, dummyID, dummyName);
counter++;cout << "inside" << endl;
}
else
{
Node<NumType> *newNode = new Node<NumType>(NULL, tests, dummyID, dummyName);
Node<NumType> *first = new Node<NumType>(NULL, tests, dummyID, dummyName);
Node<NumType> *second;
first = this->head;
second = this->head->getNext();
//create location() method to handle this!
for(int x = 0; x < counter; x++)
{
if(first->getID() > newNode->getID())
{
head = newNode;
counter++;
break;
}
else if(first->getID() < newNode->getID() && second->getID() > newNode->getID())
{
newNode->setNext(second);
first->setNext(newNode);
counter++;
break;
}
else if(second->getID() < newNode->getID() && second->getNext() == NULL)
{
second->setNext(newNode);
counter++;
break;
}
else
{
first = second;
second = second->getNext();
}
}
}
}
Node<NumType> * temp = head;
for(int x = 0; x <= counter; x++)
{
NumType *arr;
cout << temp->getID() << endl << temp->getName() << endl;
arr = temp->getAllScores();
for(int y = 0; y <3 ; y++)
cout << *(arr+y) << endl;
temp = temp->getNext();
}
}
//Node.h
#ifndef NODE_H
#define NODE_H
#include <cstdlib>
#include <iostream>
using namespace std;
template <class ItemType>
class Node
{
static const int SIZE = 3;
int ID;
ItemType scores[SIZE];
string name;
Node *next;
public:
Node()
{
this->scores[0] = 0;
this->scores[1] = 0;
this->scores[2] = 0;
this->name = "";
this->ID = 0;
this->next = NULL;
}
Node(Node * nPtr, ItemType tests[], int num, string n)
{
this->next = nPtr;
for(int z = 0; z < SIZE; z++)
this->scores[z] = tests[z];
this->ID = num;
this->name = n;
}
~Node(){}
void setNext( Node * );
string getName();
int getID();
ItemType* getAllScores();
Node* getNext();
};
#include "Node.cpp"
#endif
Node.cpp
#include "Node.h"
#include <string>
using namespace std;
template <class ItemType>
void Node<ItemType> :: setNext( Node * nextPtr)
{
cout << "inside setNext()" << endl;
this->next = nextPtr;
cout << "exited setNext()" << endl;
}
template <class ItemType>
string Node<ItemType> :: getName()
{
return (this->name);
}
template <class ItemType>
int Node<ItemType> :: getID()
{
return (this->ID);
}
template <class ItemType>
ItemType* Node<ItemType> :: getAllScores()
{
return (this->scores);
}
template <class ItemType>
Node<ItemType> * Node<ItemType> :: getNext()
{
return (this->next);
}

I think I found the error. The first time you run the add method head is set, but the second time you are trying to set the second value. You have this code
first = this->head;// this is the first element
second = this->head->getNext(); // this is null (hast been assign yet
then you go inside of a for loop, and in the first "else if" statement you have this:
else if(first->getID() < newNode->getID() && second->getID() > newNode->getID())
when you say second->getID() you are saying null->getID() causing you a segmentation fault.
I hope this fixes your problem. Good luck!

Related

C++ adding one line of code after while loop causes an error

I have tried my best to look for an answer that addresses my specific question but unfortunately, I wasn't able to find one that satisfied my needs.
I am writing an assembler in c++ for a language that I made up. Writing the assembler involves two classes that have been causing problems really tough to debug. My debugging techniques have devolved to adding and removing output stream commands like
std::cout << "TEST";
which end up either breaking or fixing the program. The IDE I am using is Xcode.
The two classes that I am working on are a LinkedList class and a SymbolTable class, which I am using as a hash table for symbol resolution.
LinkedList.h
#include <iostream>
using namespace std;
struct Node{
string data;
int address;
Node* next = nullptr;
};
class LinkedList{
private:
Node* main_ptr;
int length;
public:
LinkedList();
void insertNode(string, int);
void deleteNode(string);
void displayList();
bool contains(string, int* = nullptr);
int getLength();
~LinkedList();
};
LinkedList.cpp
#include "LinkedList.h"
LinkedList::LinkedList(){
main_ptr = nullptr;
length = 0;
}
void LinkedList::insertNode(string data, int address){
Node* newNode = new Node;
newNode->data = data;
newNode->address = address;
if (!main_ptr){
main_ptr = newNode;
} else {
newNode->next = main_ptr;
main_ptr = newNode;
}
length++;
}
void LinkedList::deleteNode(string data){
if (main_ptr){
int current_length = length;
if (main_ptr->data == data){
main_ptr = main_ptr->next;
return;
}
Node* q = main_ptr;
Node* p = q;
q = q->next;
while (q){
if (q->data == data){
p->next = q->next;
length--;
return;
}
p = q;
q = q->next;
}
if (current_length == length) cout << "Node was not found" << endl;
} else {
cout << "List is empty, cannot delete node!" << endl;
}
}
void LinkedList::displayList(){
if (!main_ptr){
cout << "List is empty!\n";
} else {
Node* temp = main_ptr;
while (temp){
cout << temp->data;
temp = temp->next;
}
cout << endl;
}
}
bool LinkedList::contains(string data, int* address){
if (main_ptr == nullptr) return false;
else {
Node* temp = main_ptr;
while(temp != nullptr){
if (temp->data == data) {
address = &(temp->address);
return true;
}
temp = temp->next;
}
return false;
}
}
int LinkedList::getLength(){
return length;
}
LinkedList::~LinkedList(){
if (main_ptr){
Node* q = main_ptr;
Node* p = q;
while (q){
q = q->next;
delete p;
p = q;
}
}
}
SymbolTable.h
#include "LinkedList.h"
#include <iostream>
class SymbolTable{
private:
LinkedList table[701];
public:
SymbolTable();
void addEntry(string, int);
void printTable();
bool contains(string, int*);
int convertName(string);
};
SymbolTable.cpp
#include "SymbolTable.h"
SymbolTable::SymbolTable(){
}
void SymbolTable::addEntry(string name, int memory){
int address = convertName(name);
table[address].insertNode(name, memory);
}
void SymbolTable::printTable(){
for (int i = 0; i < 701; i++)
table[i].displayList();
}
bool SymbolTable::contains(string name, int* memory){
return table[convertName(name)].contains(name, memory);
}
int SymbolTable::convertName(string name){
int aggregate = 1;
const char* c_name = name.c_str();
for (int i = 0; i < name.length(); i++){
aggregate *= (int)c_name[i];
}
return aggregate%701;
}
Now comes my actual question. The following is the main function:
#include "Parser.h"
#include "SymbolTable.h"
using namespace std;
int main(int argc, const char * argv[]) {
ifstream inputFile;
ofstream outputFile;
inputFile.open("/Users/Azaldin/Desktop/nand2tetris/projects/06/max/Max.asm");
outputFile.open("/Users/Azaldin/Desktop/nand2tetris/projects/06/max/Max.hack");
Parser a(inputFile);
SymbolTable s;
int commandType = -1;
string command;
int a_position = 16;
int currentLine = 0;
while (a.hasMoreCommands()){
a.advance();
commandType = a.commandType();
command = a.symbol();
if (commandType == 0){
if (!s.contains(command, nullptr)){
s.addEntry(command, a_position);
a_position++;
}
}
if (commandType == 2){
//cout << command << endl;
if (!s.contains(command, nullptr)){
s.addEntry(command, currentLine);
//cout << command << endl;
}
}
currentLine++;
}
string x; //<<<<<<<<<< ADDING THIS LINE BREAKS THE PROGRAM
cout << "Hi";
inputFile.close();
outputFile.close();
return 0;
}
After adding the line pointed to above in the main function, the program breaks.
Upon debugging, the following problem arises from the main thread:
Thread 1:EXC_BAD_ACCESS (code = 1, address=0x25fbfef80)
The chain of commands that leads to this problem:
if (!s.contains(command, nullptr)){ from the main function
return table[convertName(name)].contains(name, memory); from the SymbolTable.cpp "contains" function
if (temp->data == data) { from LinkedList.cpp "contains" function
The rest of the chain command leads to the string class == operator, which then leads to the .size() function on the left-hand side.
Thank You!
The following comment by PaulMcKenzie helped solve the problem:
Also, aggregate *= (int)c_name[i]; -- there is no guarantee that this
will be a positive number, since a character may be signed, thus
giving you values of -1 and below. Thus your return of return
aggregate%701; isn't going to return what you expected (a number >=
0).
It turns out that one of the arguments passed into the convertName function in the SymbolTable class have caused the program to corrupt the memory. I added an if statement to solve the problem.

How do I put Hash Tables into a Stack in C++?

I'm trying to make a stack of hash tables with the push and pop functions. I have the push and pop down, but is there a way to modify the stack to put H1 and H2 into Stack One? Currently, my stack holds ints for testing purposes.
Below are all headers and source files. I'd really like a solution for this issue. Thanks.
Stack.h
#ifndef STACK_H
#define STACK_H
#include <string>
using namespace std;
class Stack
{
private:
struct item
{
int value;
item* prev;
};
item* stackPtr;
public:
void Push(int value);
void Pop();
void ReadItem(item* r);
void Print();
Stack(); //called when stack is created
~Stack();//called when stacked item is ended
};
#endif /* STACK_H */
Stack.cpp
#include <iostream>
#include <cstdlib>
#include "Stack.h"
using namespace std;
Stack::Stack()
{
stackPtr = NULL;
}
Stack::~Stack()
{
item* p1; //pointer 1
item* p2; //pointer 2
p1 = stackPtr;
while(p1 != NULL)
{
p2 = p1;
p1 = p1->prev;
p2->prev = NULL;
delete p2;
}
}
void Stack::Push(int value)
{
item* n = new item;
n->value = value;
if(stackPtr == NULL)
{
stackPtr = n;
stackPtr->prev = NULL;
}
else
{
n->prev = stackPtr;
stackPtr = n;
}
}
void Stack::ReadItem(item* r)
{
cout << "value: " << r->value << endl;
cout << "------------------\n";
}
void Stack::Pop()
{
if(stackPtr == NULL)
{
cout << "The stack is empty\n";
}
else
{
item* p = stackPtr;
ReadItem(p);
stackPtr = stackPtr->prev;
p->prev = NULL;
delete p;
}
}
void Stack::Print()
{
item* p = stackPtr;
while(p != NULL)
{
ReadItem(p);
p = p->prev;
}
}
Hashtable.h
#include <cstdlib>
#include <iostream>
#include <string>
#ifndef HASHTABLE_H
#define HASHTABLE_H
class HashTable
{
private:
static const int tableSize = 10;
//everything in the braces are what makes the item
struct obj
{
int name;
obj* next;
};
obj* HASHTBL[tableSize];
public:
HashTable();
//Hash is the function represents where in the hash table
//we will store the key
//take a string stored in variable
int Hash( int key);
void AddObj(int name);
int ItemsinBucket(int index);
void PrintTable();
};
#endif /* HASHTABLE_H */
Hashtable.cpp
#include <cstdlib>
#include <iostream>
#include <string>
#include "HashTable.h"
using namespace std;
//Takes from the HashTable class in HashTable.h
HashTable::HashTable()
{
for(int x = 0; x < tableSize; x++)
{
HASHTBL[x] = new obj;
HASHTBL[x]->name = NULL;
HASHTBL[x]->next = NULL;
}
}
void HashTable::AddObj( int name)
{
int index = Hash(name);
if(HASHTBL[index]->name == NULL)
{
HASHTBL[index]->name = name;
}
else
{
obj* Ptr = HASHTBL[index];
obj* n = new obj;
n->name = name;
n->next = NULL;
while(Ptr->next !=NULL)
{
Ptr = Ptr->next;
}
Ptr->next = n;
}
}
int HashTable::ItemsinBucket(int index)
{
int count = 0;
if (HASHTBL[index]->name == NULL)
{
return count;
}
else
{
count++;
obj* Ptr = HASHTBL[index];
while(Ptr->next != NULL)
{
count++;
Ptr = Ptr->next;
}
}
return count;
}
void HashTable::PrintTable()
{
int number;
for(int x = 0; x< tableSize; x++)
{
number = ItemsinBucket(x);
cout << "------------------------\n";
cout << "index = " << x << endl;
cout << HASHTBL[x]->name << endl;
cout << "The num of items in index = "<< number << endl;
cout << "------------------------\n";
}
}
int HashTable::Hash(int key)
{
//defining HashTable function:
int HashTable = 0;
int index;
//will return integer value...or length of string you pass in
for(int x = 0; x < key-1; x++)
{
HashTable = HashTable + x;
}
//so the hash table will take a number and mod it to return the remainder
//the remainder is the index
index = HashTable % tableSize;
return index;
}
main.cpp
#include <iostream>
#include <cstdlib>
#include "Stack.h"
#include "HashTable.h"
using namespace std;
int main(int argc, char** argv)
{
Stack One;
One.Push(3);
One.Push(0);
One.Push(4);
One.Push(5);
HashTable H1;
H1.AddObj(4);
H1.AddObj(23);
H1.AddObj(200);
H1.AddObj(10);
H1.AddObj(15);
H1.AddObj(42);
H1.AddObj(33);
H1.AddObj(44);
H1.AddObj(55);
H1.AddObj(5);
H1.AddObj(9);
H1.AddObj(90);
HashTable H2;
H2.AddObj(10);
H2.AddObj(90);
H2.AddObj(99);
H2.AddObj(34);
H2.AddObj(88);
H2.AddObj(14);
H2.AddObj(87);
H2.AddObj(18);
H2.AddObj(54);
H2.AddObj(56);
H2.AddObj(6);
H2.AddObj(2);
One.Print();
cout << "\n\n\n";
H1.PrintTable();
/*
cout << "1. Push Stack Element" << endl;
cout << "2. Pop Stack Element" << endl;
cout << "3. Search Hash Table" << endl;
*/
return 0;
}

new operator is not allocating memory properly.

my code is suppose to create a singly linked list using and array of nodes.
each Node has variable item which hold data and variable next which holds the index of the next node in the list. the last node has -1 in its next data field to simulate a nullptr. head holds the index of the first node in the list.
for some reason when i try to create new memory the program throws an instance of bad_alloc
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
#include "ArrayList.h"
#include <iostream>
using namespace std;
ArrayList::ArrayList(char ch){
array = new Node[Size];
(array[0]).item = ch;
(array[0]).next = 1;
free = 1;
head = 0;
}
bool ArrayList::cons(char ch) {
if (this->isFull()){
this->doubleArray();
}
(array[free]).item = ch;
(array[free]).next = head;
head = free;
free++;
// cout << free << endl;
return true;
}
bool ArrayList::isFull() const{
int current = head;
int count =0;
while( (array[current]).next != -1 ){
count++;
current = (array[current]).next;
}
return (count == Size) ? true : false;
}
bool ArrayList::doubleArray() {
int oldSize = Size;
Size *=2;
Node* newArray = new Node[Size]; // problem occurs here, i think
int count =0;
while(oldSize >= count ){
(newArray[count]).item = (array[count]).item;
(newArray[count]).next = (array[count]).next;
count++;
}
free = count +1;
delete [] array;
array = newArray;
cout <<"free: "<< free << endl;
return true; // come up with thiss
}
void ArrayList::print() const{
if (head == -1) return;
int current = head;
cout << "[ ";
while( (array[current]).next != -1){
cout << (array[current]).item <<" ";
current = (array[current]).next;
}
cout << (array[current]).item <<"]";
return;
}
///////////////////////////////////
#ifndef ARRAYLIST_H
#define ARRAYLIST_H
#include <iostream>
using namespace std;
class Node{
public:
char item;
int next;
Node(){
next = -1;
}
Node(char input){
this->item = input;
next = -1;
}
};
class ArrayList{
public:
ArrayList(char ch);
int length() const;
void print() const;
private:
bool doubleArray();
bool isFull() const;
Node* array;
int Size = 5;
int head = -1;
int free = 0;
};
#endif
////////////////////////////////////////
#include <iostream>
#include "ArrayList.h"
using namespace std;
int main(){
ArrayList list('1');
list.cons('2');
list.cons('3');
list.cons('4');
list.cons('5');
list.cons('6');
list.cons('7');
list.print();
//cout << list.length();
return 0;
}

Read integers into a simple linked list from text file. Then bubblesort the list of integers and read out to another file

Read integers into a simple linked list from text file. Then bubblesort the list of integers and read out to another file. Right now I am reading into the main but I am trying to overload the extraction operator to read it in and I am not sure how to go about that. My Bubblesort function is also causing alot of issues. Its telling me the function cannot be overloaded and the node identifier is undeclared amongst other things. Any help would be greatly appreciated
Main file
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <fstream>
#include "bubble.h"
using namespace std;
struct nodeType
{
int info;
nodeType* link;
};
node *head_ptr = NULL;
void Display();
void list_clear(nodeType*& head_ptr);
void list_copy(const nodeType* source_ptr, nodeType*&head_ptr, nodeType*&tail_ptr);
Bubblesort();
int main()
{
ifstream datld;
ofstream outld;
Bubble D3;
datld.open ("infile2.txt");
if (!datld)
{
cout << "failure to open data.txt" << endl;
system ("pause");
return 1;
}
datld >> D3;
while(datld)
{
cout << D3<< endl;
datld >> D3;
}
system("pause");
return 0;
Bubblesort();
}
void Bubblesort()
{
node* curr = head_ptr;
int count = 0;
while(curr!=NULL)
{
count++;
curr = curr->NEXT;
}
for(int i = count ; i > 1 ; i-- )
{
node *temp, *swap1;
swap1 = HEAD;
for(int j = 0 ; j < count-1 ; j++ )
{
if(swap1->DATA > swap1->NEXT->DATA)
{
node *swap2 = swap1->NEXT;
swap1->NEXT = swap2->NEXT;
swap2->NEXT = swap1;
if(swap1 == HEAD)
{
HEAD = swap2;
swap1 = swap2;
}
else
{
swap1 = swap2;
temp->NEXT = swap2;
}
}
temp = swap1;
swap1 = swap1->NEXT;
}
}
}
void list_clear(nodeType*& head_ptr)
//Library facilities used:cstdlib
{
nodeType * removeptr;
while(head_ptr!=NULL)
{
removeptr=head_ptr;
head_ptr=head_ptr->link;
delete removeptr;
}
}
void list_copy(const nodeType* source_ptr, nodeType*&head_ptr, nodeType*&tail_ptr)
{
nodeType* temp;// to allocate new nodes
head_ptr=NULL;
tail_ptr=NULL;
if(source_ptr==NULL)
return;
head_ptr=new nodeType;
head_ptr->link=NULL;
head_ptr->info=source_ptr->info;
tail_ptr=head_ptr;
source_ptr=source_ptr->link;
while(source_ptr!=NULL)
{
temp = new nodeType;
temp->link=NULL;
temp->info =source_ptr-> info;
tail_ptr->link=temp;
tail_ptr = tail_ptr->link;
source_ptr = source_ptr->link;
}
}
Header file
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
using namespace std;
class Bubble
{
private:
int manynodes;
public:
Bubble() { }
void Bubblesort();
friend ostream &operator<<( ostream &output, const Bubble &D )
{
output << D.manynodes;
return output;
}
friend istream &operator>>( istream &input, Bubble &D )
{
input >> D.manynodes;
return input;
}
};
Your extraction operator looks fine. I don't know if it really does what you need, but that's another issue.
You are declaring function Bubblesort() twice: first in the header file as void Bubblesort(), then in the main file as just Bubblesort() (this should at least give you a warning that it is considered to mean int Bubblesort()). You cannot overload a function just on the return value, hence the error.
Indeed, you are using a type called node in several places, but you have not declared nor defined it anywhere.

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;
}