Click to See Complete Forum and Search --> : operator overloading


npis2001
April 26th, 2004, 01:32 PM
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::preOrder()
{
preOrder(root);
}
void BTree::preOrder(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::postOrder()
{
postOrder(root);
}
void BTree::postOrder(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

tomcant
April 26th, 2004, 01:37 PM
I think you should first format your code so that it is readable, then put your code in code tags. It helps ;)

TheCPUWizard
April 26th, 2004, 01:42 PM
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.

npis2001
April 26th, 2004, 01:48 PM
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

cma
April 26th, 2004, 10:15 PM
It's because of the function setfill(0). Try changing it to:

os << setfill('0') << ....

Graham
April 27th, 2004, 03:32 AM
Also, it's <iostream> and <iomanip> - note no ".h"

treuss
April 27th, 2004, 03:39 AM
Originally posted by npis2001
ostream& operator<<(ostream &os, BTreeNode& rBTN)
{
[...]

Better:
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.

ScreechPowers
April 28th, 2004, 03:06 PM
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