Pointer Question

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2004
Posts: 348
Reputation: paradox814 is an unknown quantity at this point 
Solved Threads: 4
paradox814's Avatar
paradox814 paradox814 is offline Offline
Posting Whiz

Pointer Question

 
0
  #1
May 11th, 2008
I am stuck on something that I know is so simple that I can't figure it out...

I have a BST class, and a private item:
  1. Node<K> *root; // root pointer
  2.  
  3.  
  4. //and in a seperate class I have...
  5. template <typename K>
  6. class Node
  7. {
  8. public:
  9.  
  10. // constructors
  11. Node() {};
  12. Node(const K newKey, Node<K> *l, Node<K> *r)
  13. :key(newKey), left(l), right(r) {};
  14.  
  15. // data members
  16. K key; // key is a template
  17. Node<K> * left; // points to left child or NULL
  18. Node<K> * right; // points to right child or NULL
  19. };
  20.  
  21. //cut&paste some code
  22. Node<K> n;
  23. n.key = newKey;
  24. n.left = NULL;
  25. n.right = NULL;
  26. //so obviously there is a lot of cut&paste going on, but I assure you everything above this line works just fine.
  27. root = &n;


Any help is greatly appreciated!!! After a certain point I tried every combination of &* that I could think of, ie: root = *n; *root=n; *root=&n; *root=*n; ... etc, you get the point!

Any help is greatly appreciated!!!
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 675
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 99
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: Pointer Question

 
0
  #2
May 11th, 2008
Isnt it working with

*root=n;// As this is a normal code for pointing to a variable?
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 248
Reputation: hammerhead is an unknown quantity at this point 
Solved Threads: 24
hammerhead's Avatar
hammerhead hammerhead is offline Offline
Posting Whiz in Training

Re: Pointer Question

 
0
  #3
May 11th, 2008
You have created Node<K> *root; before defining the class Node. Declare it after the class. Furthermore I dont think you can use Node<K> n; outside the class. You will have to specify a valid class or datatype instead of template K like Node<int> n;
There are 10 types of people in the world, those who understand binary and those who don't.

All generalizations are wrong. Even this one.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 224
Reputation: bugmenot is an unknown quantity at this point 
Solved Threads: 31
bugmenot bugmenot is offline Offline
Posting Whiz in Training

Re: Pointer Question

 
0
  #4
May 12th, 2008
how about
  1. root = new Node<K>(newKey, NULL, NULL);
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC