default variable as a pointer in class

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

Join Date: Apr 2008
Posts: 3
Reputation: Gondt is an unknown quantity at this point 
Solved Threads: 0
Gondt Gondt is offline Offline
Newbie Poster

default variable as a pointer in class

 
0
  #1
Apr 20th, 2008
the title describes the problem. here's how the code looks like:

  1. #include <cstdio>
  2.  
  3. class example
  4. {
  5. public:
  6. class node
  7. {
  8. public:
  9. int value;
  10. node *left, *right;
  11. };
  12.  
  13. node *root;
  14.  
  15. example (int a)
  16. {
  17. root=new node;
  18. root->value=a;
  19. }
  20.  
  21. // HERE'S THE PROBLEM:
  22. void function (node *variable=root)
  23. {
  24.  
  25. }
  26. };
  27.  
  28. int main()
  29. {
  30.  
  31. }

g++ returns:
  1. p.cpp:13: error: invalid use of non-static data member 'przyklad::root'
  2. p.cpp:21: error: from this location

i don't want to use static... do you know what is the proper way of coding it?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,632
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1497
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: default variable as a pointer in class

 
0
  #2
Apr 20th, 2008
Here is an explaination of default parameter values. That link talks about exactly what you are trying to do.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 3
Reputation: Gondt is an unknown quantity at this point 
Solved Threads: 0
Gondt Gondt is offline Offline
Newbie Poster

Re: default variable as a pointer in class

 
0
  #3
Apr 20th, 2008
i cannot find the answer there. i have no problem in using default parameters, except this particular case.

may it be somewhat connected to the fact, that if i put there
  1. void function (node *variable=this->root)
i get
  1. p.cpp:21: error: 'this' may not be used in this context
a correct code would be highly appreciated
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,632
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1497
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: default variable as a pointer in class

 
0
  #4
Apr 20th, 2008
>> cannot find the answer there.
Click on the link "Restrictions on Default Arguments" in the left pannel.
You cannot use local variables in default argument expressions. For example, the compiler generates errors for both function g() and function h() below:
  1. void f(int a)
  2. {
  3. int b=4;
  4. void g(int c=a); // Local variable "a" cannot be used here
  5. void h(int d=b); // Local variable "b" cannot be used here
  6. }
>>a correct code would be highly appreciated
The only correct way to do that is to make the local variable static.
Last edited by Ancient Dragon; Apr 20th, 2008 at 12:44 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 3
Reputation: Gondt is an unknown quantity at this point 
Solved Threads: 0
Gondt Gondt is offline Offline
Newbie Poster

Re: default variable as a pointer in class

 
0
  #5
Apr 20th, 2008
solved, thank you for your help
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



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC