I am getting this error:
'Print' : overloaded member function 'void (class ostream &) const' not found in 'btree'
I have this function in my header:
void Print(ofstream& fout1) const;
I can't understand why I am getting this error.
I am getting this error:
'Print' : overloaded member function 'void (class ostream &) const' not found in 'btree'
I have this function in my header:
void Print(ofstream& fout1) const;
I can't understand why I am getting this error.
Maybe post a little more code. Is this a function or a method?
Hm...
I'm just learning C++ myself but I would try the following:
ostream &Print( ostream &fout1);
If you return the stream, you can overload the function for more than one item. As I recall, on Dev-C++ you get a similar error message when you don't return the stream on multiple items.
Example (assume cout is overloaded):
cout << ob1 << ob2; // legal if you return the stream
cout << ob1; // legal if you do or don't return stream
cout << ob1 << ob2; // illegal if you don't return the stream
This is locking up. I have to have a print function because I have to format the output in a certain way. The errror points to this line->bt.Print(fout,leaf);
#include<iostream>
#include<fstream>
#include<iomanip>
#include"BSTTreeAndyJackson.cpp"
using namespace std;
int main()
{
btree bt;
int num=0;
char chr;
int count=0;
node *leaf;
// ifstream fin;
// ofstream fout;
fin.open("input.txt");
fout.open("output1.txt");
fin>>num;
fin>>chr;
while(fin)
{
count++;
bt.insert(num);
fout << "InOrderTranverse = ";
bt.InOrderTranverse();
bt.Print(fout,leaf);
fout<<endl<<endl;
//fout<<"Preorder Transversal = "<<endl<<endl;
bt.preOrderTranverse();
//fout << endl<<endl;
//fout<<"Step: "<<count<<" "<<"Data Value = "<<num<<" "<<"Activity Signal = "<<chr<<endl;
//fout<<"Post order Transversal = "<<endl<<endl;
//fout<<"Level by Level Transversal = "<<endl<<endl;
//fout<<"Total Number of nodes = "<<count<<endl<<endl<<endl;
fin>>num;
fin>>chr;
}
return 0;
}
#include"BSTTreeAndyJackson.h"
#include<iostream>
//#include<fstream.h>
//ifstream fin("input.txt");
// ofstream fout("output1.txt");
ifstream fin;
ofstream fout;
//Constructor
///////////////////////////////////////////////////////////////////////
btree::btree()
{
root=NULL;
}
//Insert Tree
//////////////////////////////////////////////////////////////////////
void btree::insert(int key, node *leaf)
{
if(key< leaf->key_value)
{
if(leaf->left!=NULL)
insert(key, leaf->left);
else
{
leaf->left=new node;
leaf->left->key_value=key;
leaf->left->left=NULL; //Sets the left child of the child node to null
leaf->left->right=NULL; //Sets the right child of the child node to null
}
}
else if(key>=leaf->key_value)
{
if(leaf->right!=NULL)
insert(key, leaf->right);
else
{
leaf->right=new node;
leaf->right->key_value=key;
leaf->right->left=NULL; //Sets the left child of the child node to null
leaf->right->right=NULL; //Sets the right child of the child node to null
}
}
}
///////////////////////////////////////////////////////////////////////
void btree::insert(int key)
{
if(root!=NULL)
insert(key, root);
else
{
root=new node;
root->key_value=key;
root->left=NULL;
root->right=NULL;
}
}
//////////////////////////////////////////////////////////////////////
void btree::InOrderTranverse(node *leaf)
{
if(leaf->left != NULL)
InOrderTranverse(leaf->left);
//fout<<leaf->key_value<< " ";
if(leaf->right !=NULL)
InOrderTranverse(leaf->right);
}
//////////////////////////////////////////////////////////////////////
void btree::InOrderTranverse()
{
if(root!=NULL)
InOrderTranverse(root);
else
return;
}
//////////////////////////////////////////////////////////////////////
void btree:reOrderTranverse(node *leaf)
{
if(leaf != NULL){
//fout<<leaf->key_value;
preOrderTranverse(leaf->left);
preOrderTranverse(leaf->right);
}
}
///////////////////////////////////////////////////////////////////////
void btree:reOrderTranverse()
{
if(root!=NULL)
preOrderTranverse(root);
else
return;
}
//////////////////////////////////////////////////////////////////////
void btree::Print(ofstream& fout, node *leaf) const
{
fout<<leaf->key_value;
}
// while(root != NULL)
// {
// fout<<leaf->key_value;
// }
#include<fstream.h>
struct node;
typedef node *Nodeptr;
struct node
{
int key_value;
node *left;
node *right;
};
class btree
{
public:
btree();
void insert(int key);
void InOrderTranverse();
void preOrderTranverse();
void Print(ofstream& fout, node *leaf) const;
private:
void insert(int key, node *leaf);
void InOrderTranverse(node *leaf);
void preOrderTranverse(node *leaf);
//Nodeptr *root;
node *root;
};
That's because leaf doesn't point to anything. If you want to ouput the node that's just been inserted you'll have to return a pointer to a node from your insert function (as your public insert function just wraps the private insert implementation you'll have to return the node through the functions) and pass that pointer to the print function.The errror points to this line->bt.Print(fout,leaf);
zen