CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2004
    Posts
    8

    Angry operator overloading

    VC++ 6.0 complier complaining of ambiguous symbol and calls when i overload this function:

    ostream& operator<<(ostream &os, BTreeNode& rBTN)

    Source file:

    #include <iostream.h>
    #include <iomanip.h>
    #include "BSTree.h"

    using namespace std;


    BTreeNode::BTreeNode(int v)
    {
    value=v;
    left=0;
    right=0;
    }
    int BTreeNode::getValue()
    {
    return value;
    }
    BTreeNode* BTreeNode::getLeft()
    {
    return left;
    }
    BTreeNode* BTreeNode::getRight()
    {
    return right;
    }
    BTreeNode** BTreeNode::getLeftAddr()
    {
    return &left;
    }
    BTreeNode** BTreeNode::getRightAddr()
    {
    return &right;
    }
    // complains about this overloading function!
    ostream& operator<<(ostream &os, BTreeNode& rBTN)
    {

    os << setfill(0)<<"node: "<< setw(6) << &rBTN
    <<"value: "<< setw(5) << rBTN.getValue()
    <<"left: "<< setw(6) << rBTN.getLeft()
    <<"right: "<< setw(6) << rBTN.getRight();

    return os;
    }



    BTree::BTree()
    {
    root=0;
    }

    void BTree::insert(int val)
    {
    insert(val, &root);
    }

    void BTree::insert(int val, BTreeNode **bT)
    {
    if(*bT==0)
    {
    *bT=new BTreeNode(val);
    if(bT==0)
    {
    cout<<"something majorly wrong here";
    }
    }
    else if(val<(*bT)->getValue())
    {
    insert(val, (*bT)->getLeftAddr());
    }
    else if(val>(*bT)->getValue())
    {
    insert(val, (*bT)->getRightAddr());
    }
    }

    int BTree::find(int val)
    {
    return find(val, root);
    }

    int BTree::find(int val, BTreeNode *bT)
    {
    int rtrn=FALSE;

    if(bT==0)
    {}
    else if(val<(bT)->getValue( ))
    {
    rtrn=find(val, (bT)->getLeft( ));
    }
    else if(val>(bT)->getValue( ))
    {
    rtrn=find(val, (bT)->getRight( ));
    }
    else
    {
    rtrn=TRUE;
    }
    return rtrn;
    }

    void BTree:reOrder()
    {
    preOrder(root);
    }
    void BTree:reOrder(BTreeNode *bT)
    {
    if(bT!=0)
    {
    cout<<bT->getValue();
    preOrder(bT->getLeft( ));
    preOrder(bT->getRight( ));
    }
    else
    cout<<"bT is equal to 0";
    }
    void BTree::inOrder()
    {
    inOrder(root);
    }
    void BTree::inOrder(BTreeNode *bT)
    {
    if(bT!=0)
    {

    inOrder(bT->getLeft( ));
    cout<<bT->getValue()<<"\n";
    inOrder(bT->getRight( ));
    }
    }
    void BTree:ostOrder()
    {
    postOrder(root);
    }
    void BTree:ostOrder(BTreeNode *bT)
    {
    if(bT!=0)
    {

    postOrder(bT->getLeft( ));
    postOrder(bT->getRight( ));
    cout<<bT->getValue();
    }
    else
    cout<<"bT is equal to 0";
    }

    header file:

    #include <iomanip>


    #ifndef BSTree_H
    #define BSTree_H
    //declare BTreeNode as a class

    const FALSE = 0;
    const TRUE = 1;

    class BTreeNode
    {
    //public data
    public:
    BTreeNode(int);
    int getValue();
    BTreeNode* getLeft();
    BTreeNode* getRight();
    BTreeNode** getLeftAddr();
    BTreeNode** getRightAddr();


    //private data
    private:
    int value;
    BTreeNode* left;
    BTreeNode* right;
    };
    class BTree
    {
    public:
    BTree();
    void insert(int);
    int find(int);
    void preOrder();
    void inOrder();
    void postOrder();
    //BTreeNode *root;


    private:
    BTreeNode *root;

    void insert(int, BTreeNode**);
    int find(int, BTreeNode*);
    void preOrder(BTreeNode*);
    void inOrder(BTreeNode*);
    void postOrder(BTreeNode*);
    };

    #endif

  2. #2
    Join Date
    Apr 2004
    Location
    Colchester, UK
    Posts
    97
    I think you should first format your code so that it is readable, then put your code in code tags. It helps

  3. #3
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125
    Also little things like the relevant header files.....

    note: In general the IDEAL way to post a "why is this not working" is to reduce the program to the absolute minimum number of lines that exhibit the problem, then post the code in a compilable form. Many (especially myself) are more likely to spend time on a issue if it is obvious that the poster has done his/her best to present the problem.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  4. #4
    Join Date
    Apr 2004
    Posts
    8
    Thanks guys.


    ostream& operator<<(ostream &os, BTreeNode& rBTN)
    {

    os << setfill(0)<<"node: "<< setw(6) << &rBTN
    <<"value: "<< setw(5) << rBTN.getValue()
    <<"left: "<< setw(6) << rBTN.getLeft()
    <<"right: "<< setw(6) << rBTN.getRight();

    return os;
    }

    compiles and work fine without this overloaded operator.

    Compiler errors

    Compiling...
    BSTree.cpp
    c:\hw\266\lab12\bstree.cpp(34) : error C2872: 'ostream' : ambiguous symbol
    c:\hw\266\lab12\bstree.cpp(34) : error C2872: 'ostream' : ambiguous symbol
    c:\hw\266\lab12\bstree.cpp(37) : error C2668: 'setw' : ambiguous call to overloaded function
    c:\hw\266\lab12\bstree.cpp(38) : error C2668: 'setw' : ambiguous call to overloaded function
    c:\hw\266\lab12\bstree.cpp(39) : error C2668: 'setw' : ambiguous call to overloaded function
    c:\hw\266\lab12\bstree.cpp(40) : error C2668: 'setw' : ambiguous call to overloaded function

  5. #5
    Join Date
    Feb 2004
    Location
    USA - Florida
    Posts
    729
    It's because of the function setfill(0). Try changing it to:
    Code:
    os << setfill('0') << ....
    Hungarian notation, reinterpreted? http://www.joelonsoftware.com/articles/Wrong.html

  6. #6
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    Also, it's <iostream> and <iomanip> - note no ".h"
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  7. #7
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401
    Originally posted by npis2001
    ostream& operator<<(ostream &os, BTreeNode& rBTN)
    {
    [...]
    Better:
    Code:
    ostream& operator<<(ostream &os, const BTreeNode& rBTN)
    because:
    A) Printing the object should not change it
    B) You want to be able to print non-changeable (const) objects.

  8. #8
    Join Date
    Apr 2004
    Posts
    19
    remember that when you overload an operator, you do it for a new object (for example: adding a class of large numbers) or something like it where, if applied, the compiler would not know what to do, in which case it would look to whatever you wrote if the parameters are the same
    if the operator is overloaded for use with the same data type as when it is not overloaded, the compiler wont know what to do.
    it may be more useful to just make a new function...call it insert() or something

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured