943,522 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 639
  • C++ RSS
May 11th, 2008
0

Pointer Question

Expand Post »
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:
C++ Syntax (Toggle Plain Text)
  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!!!
Similar Threads
Reputation Points: 13
Solved Threads: 4
Posting Whiz
paradox814 is offline Offline
351 posts
since Oct 2004
May 11th, 2008
0

Re: Pointer Question

Isnt it working with

*root=n;// As this is a normal code for pointing to a variable?
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008
May 11th, 2008
0

Re: Pointer Question

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;
Reputation Points: 46
Solved Threads: 24
Posting Whiz in Training
hammerhead is offline Offline
248 posts
since May 2006
May 12th, 2008
0

Re: Pointer Question

how about
C++ Syntax (Toggle Plain Text)
  1. root = new Node<K>(newKey, NULL, NULL);
Reputation Points: 53
Solved Threads: 33
Posting Whiz in Training
bugmenot is offline Offline
224 posts
since Nov 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Help with finishing a project
Next Thread in C++ Forum Timeline: cgi page reload problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC