Thread: Print Function

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    84

    Print Function

    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.

  2. #2
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Maybe post a little more code. Is this a function or a method?

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    11
    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

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    84

    I have a new errror?

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

  5. #5
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    The errror points to this line->bt.Print(fout,leaf);
    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.
    zen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  3. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  4. Creating a student grade book-how?
    By Hopelessly confused in forum C Programming
    Replies: 5
    Last Post: 10-03-2002, 08:43 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM