PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > References: I need a quick bit of .
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
References: I need a quick bit of .

Réponse
 
LinkBack Outils de la discussion
Vieux 16/10/2007, 17h36   #1
xkenneth
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut References: I need a quick bit of .

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 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 is appreciated.

Thanks,
Ken

  Réponse avec citation
Vieux 16/10/2007, 17h47   #2
Victor Bazarov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: References: I need a quick bit of .

xkenneth wrote:
> 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 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


  Réponse avec citation
Vieux 16/10/2007, 17h51   #3
xkenneth
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: References: I need a quick bit of .

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

Regards,
Ken

  Réponse avec citation
Vieux 16/10/2007, 17h54   #4
Victor Bazarov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: References: I need a quick bit of .

xkenneth wrote:
> [..]
> 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


  Réponse avec citation
Vieux 16/10/2007, 17h58   #5
xkenneth
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: References: I need a quick bit of .


> 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 Victor.

Regards,
Ken


  Réponse avec citation
Vieux 16/10/2007, 18h27   #6
Victor Bazarov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: References: I need a quick bit of .

xkenneth wrote:
> [..]
> 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


  Réponse avec citation
Vieux 16/10/2007, 20h52   #7
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: References: I need a quick bit of .

On Oct 16, 6:47 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
> 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.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

  Réponse avec citation
Vieux 16/10/2007, 20h54   #8
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: References: I need a quick bit of .

On Oct 16, 6:54 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
> xkenneth wrote:
> > [..]
> > 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.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 06h15.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14262 seconds with 16 queries