Question about references

This is a discussion on Question about references within the C++ Programming forums, part of the General Programming Boards category; Ok so I am building a tree using c++ In my remove function and I am using a reference (TNode<Whatever> ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    9

    Question about references

    Ok so I am building a tree using c++
    In my remove function
    and I am using a reference (TNode<Whatever> & elementTNode) to point to the deleted data in a node in the tree (TNode<Whatever> *)... So, when node is deleted the reference goes back to main function to print out the deleted data, saying that it deleted it. But the reference only has the name of the node and not the rest of the data to be printed out. So what I do not understand is how to make it point to the data and being able to hold onto it.

    Ex: i tried in my Tnode remove func
    elementTNode.data = data;
    elementTNode.data = this->data;

    doesnt work....
    can sum1 plz help me?

  2. #2
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    I need more to go on. Can you post the whole remove function.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Can you post your code?

    So what I do not understand is how to make it point to the data and being able to hold onto it.
    You mean, assign another pointer to the data?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    9
    Code:
    unsigned long Remove (TNode<Whatever> & elementTNode, 
                            TNode<Whatever> *& PointerInParent,
                            long fromSHB = FALSE) {
    
    if(elementTNode.data == data) {
       elementTNode.data = data;
      delete this;
    }
    so where i did elementTNode.data = data...
    doesnt work....
    Last edited by azncoolj2000; 11-18-2005 at 03:54 PM.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    You mean, you get a compiler error? A runtime one? Or what?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    9
    no errors... but when i try to delete a node in the tree, a msg is outputed to the user that the node is deleted with some data printed out to the user... that data that should be printed out doesnt print out to user, instead just has the name of node but no data from the node.

    ex output:
    Initial Symbol Table:
    Tree 1:
    occupancy is 0 elements.
    Please enter a command ((i)nsert, (l)ookup, (r)emove, (w)rite): i
    Please enter name to insert: a
    Please enter number: 1
    Please enter a command ((i)nsert, (l)ookup, (r)emove, (w)rite): i
    Please enter name to insert: b
    Please enter number: 2
    Please enter a command ((i)nsert, (l)ookup, (r)emove, (w)rite): i
    Please enter name to insert: c
    Please enter number: 3
    Please enter a command ((i)nsert, (l)ookup, (r)emove, (w)rite): r
    Please enter name to remove: b
    Student removed!!!
    name: b with studentnum: 0
    Please enter a command ((i)nsert, (l)ookup, (r)emove, (w)rite):


    clearly... node with name b has a data studentnum:2
    but it just prints out 0....
    if i do elementTNode = this i get an error that says:
    Tree.c:108: error: no match for 'operator=' in 'elementTNode = this'
    Tree.c:33: error: candidates are: TNode<UCSDStudent>&
    TNode<UCSDStudent>:: operator=(const TNode<UCSDStudent>&)
    Last edited by azncoolj2000; 11-18-2005 at 04:25 PM.

  7. #7
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    What is the data type of 'data'. If it is a value-type, I have no idea what the problem is. If it is a pointer type, then you probably need to actually obtain a copy of the data since you're deleting 'this'. And if it is an object-type (non-pointer), do you have an overloaded operator=, and if so, is it properly copying everything it needs? If you don't have an overloaded operator=, do you need one?

    To me it looks like you have an object type, and I would guess you are overloading operator=. If so, set a breakpoint in that function, and see why the student number is not being copied.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. inline asm question
    By JaWiB in forum Game Programming
    Replies: 14
    Last Post: 10-10-2003, 11:04 PM
  2. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 10:47 PM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  4. declare references to references works!
    By ManuelH in forum C++ Programming
    Replies: 4
    Last Post: 01-20-2003, 08:14 AM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21