References: I need a quick bit of help.

X

xkenneth

All,

Thanks to anyone who reads this. I am trying to implement a basic
dictionary class using a binary tree. That information is fairly
unimportant as I'm simply trying to use references to contain the node
data. When i create my tree, create it's root node, and then try to
return the data, it comes back as either garbled or the wrong data. I
have attached all of my code, any help is appreciated.

binNode.h
----------------

#ifndef BINNODE_H
#define BINNODE_H

class binNode
{ public:
binNode(binNode* left,float& item,binNode* right);
void setLeftChild(binNode* aChild);
void setRightChild(binNode* aChild);
binNode* left() const; // left child
binNode* right() const; // right child
float& nodeData() const; // current node data
private:
binNode* lchild;
float& data;
binNode* rchild;
};

#endif
-------------------

binNode.cc
-------------------
#include <iostream>
#include "binNode.h"

binNode::binNode(binNode* left,float& item,binNode* right) : data(0) {

if (left!=0) {
lchild = left;
} else {
lchild = 0;
}

if (right!=0) {
rchild = right;
} else {
rchild = 0;
}

data = item;

cout << "!" << data << endl;
cout << item << endl;
return;
}

void binNode::setLeftChild(binNode* aChild) {
if (aChild!=0) {
lchild = aChild;
}
return;
}

void binNode::setRightChild(binNode* aChild) {
if (aChild!=0) {
rchild = aChild;
}

return;
}

binNode* binNode::left() const {
return lchild;
}

binNode* binNode::right() const {
return rchild;
}

float& binNode::nodeData() const {
cout << "!!" << data << endl;

return data;
}

-------------------------
dict.h
-------------------------
#ifndef DICT_H
#define DICT_H

#include "binNode.h"

class dict
{ public:
dict();
~dict();
void insert(float& obj);
float& remove(const float& key);
float& search(const float& key) const;
void inorder() const;
private:

binNode* root;
};

#endif


-------------------------
dict.cc
-------------------------
#include "dict.h"
#include <iostream>

dict::dict() {

root = 0;

return;
}

dict::~dict() {
return;
}


void dict::insert(float& obj) {
float val = 0;
float& ref = val;

if (root==0) {

cout << "Creating root with value: " << obj << endl;


root = new binNode(0,obj,0);

ref = root->nodeData();

cout << "After creation: " << &ref << endl;

} else {
cout << "Need to create a child!" << endl;
if(root->nodeData()>=obj) {
cout << "Bigger than or equal to" << endl;
} else {
cout << "Less than" << endl;
}
}

return;
}

float& dict::remove(const float& key) {
return 0.0;
}

float& dict::search(const float& key) const {
return 0.0;
}

void dict::inorder() const {
cout << "Address: " << root << endl;
cout << root->nodeData();
return;
}
--------------------

main.cc
--------------------
#include "dict.h"
#include <iostream>

void main(void) {

float value1 = 5.0;
dict *myDict;

myDict = new dict();

myDict->insert(value1);


return;
}
--------------------

Output
--------------------
Creating root with value: 5 <- creation statement
!5 <- Value of the class private variable after creation
5 <-Value of the argument passed to the function
!!1.08526e-19 <-value when attempted to return by root->nodeData();
After creation: 0x11fffbfc8 <-Address of the reference, i was trying
to compare them
--------------------

Once again,
any help is appreciated.

Thanks,
Ken
 
V

Victor Bazarov

xkenneth said:
All,

Thanks to anyone who reads this. I am trying to implement a basic
dictionary class using a binary tree. That information is fairly
unimportant as I'm simply trying to use references to contain the node
data. When i create my tree, create it's root node, and then try to
return the data, it comes back as either garbled or the wrong data. I
have attached all of my code, any help is appreciated.

binNode.h
----------------

#ifndef BINNODE_H
#define BINNODE_H

class binNode
{ public:
[...]
float& data;
};

#endif
-------------------

binNode.cc
-------------------
#include <iostream>
#include "binNode.h"

binNode::binNode(binNode* left,float& item,binNode* right) : data(0) {
[..]

That should NOT compile. You're initialising a _reference_ to non-const
'float' using a _literal_ of type 'int' (the zero). If your compiler
does not complain, throw it away and get a better compiler.

Once you do, rethink your decision to store references in your nodes.
References are very delicate things, they cannot be "re-seated" to refer
to a different object. Once initialised, they refer to the same object
as long as they live; assigning to a reference changes the object to
which the reference refers.

V
 
X

xkenneth

That should NOT compile.
I agree.

You're initialising a _reference_ to non-const
'float' using a _literal_ of type 'int' (the zero).
How can i do this better? The compiler forces me to instantantiate it
or it will throw an error.

If your compiler
does not complain, throw it away and get a better compiler.
I'd love to, but I can't. This is a project for school and I must use
this compiler. It's an old compaq c++ compiler running on an alpha
processor....
Once you do, rethink your decision to store references in your nodes.
References are very delicate things, they cannot be "re-seated" to refer
to a different object. Once initialised, they refer to the same object
as long as they live; assigning to a reference changes the object to
which the reference refers.

I also agree that using references is a little goofy, but I must use
the header files given by my professor.

Is there any other reason why this wouldn't work?

Thanks for your help.

Regards,
Ken
 
V

Victor Bazarov

xkenneth said:
[..]
I also agree that using references is a little goofy, but I must use
the header files given by my professor.

Is there any other reason why this wouldn't work?

You will need to implement pruning and grafting correctly if you're
forced to use references. And you need to implement construction
properly (no assignment to 'data', use initialisation only).

V
 
X

xkenneth

You will need to implement pruning and grafting correctly if you're
forced to use references.
Read the fine print, i can rewrite that particular header file.

And you need to implement construction
properly (no assignment to 'data', use initialisation only).
Not needed now, but I'm not sure what you mean by this.

I changed the node data to be just float. Is there a way i can do it
better?

Thanks for your help Victor.

Regards,
Ken
 
V

Victor Bazarov

xkenneth said:
[..]
I changed the node data to be just float. Is there a way i can do it
better?

I am not sure what you mean by "better". References and values are
different. If you store references, you're able to manipulate the
original objects through the nodes of your tree. If you store the
values, the connection to the original object is severed, so you do
not have the leverage to change the original object, but should the
original object be destroyed, the node still contains valid data,
and not a reference pointing to nowhere.

V
 
J

James Kanze

xkenneth wrote:

[...]
binNode.h
----------------
#ifndef BINNODE_H
#define BINNODE_H
class binNode
{ public:
[...]
float& data;
};
#endif
-------------------

[...]
Once you do, rethink your decision to store references in your nodes.
References are very delicate things, they cannot be "re-seated" to refer
to a different object.

Especially, in this case, a float& has absolutely no advantage
over a simple float.
Once initialised, they refer to the same object as long as
they live; assigning to a reference changes the object to
which the reference refers.

I suspect that in this case, he could get by with a float
const&. But frankly, using anything but a simple float seems
very, very wrong.
 
J

James Kanze

xkenneth said:
[..]
I also agree that using references is a little goofy, but I must use
the header files given by my professor.
Is there any other reason why this wouldn't work?
You will need to implement pruning and grafting correctly if you're
forced to use references. And you need to implement construction
properly (no assignment to 'data', use initialisation only).

He can make it work exactly as if he'd used a simple float by
initializing the reference with a *new float in the constructor,
and adding a delete &data in the destructor. It's stupid to do
so, of course, but since he says the prof. insists.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top