default argument values 'require static member'

Apr 16, 2011 at 2:50am
hi


I would like to have a function defined inside a class that i can invoke with

TreeMinimumValue(nodePtrToSomeSubtree);

and/or

TreeMinimumValue(); //use the default argument value "the root of the tree"


The following code results in a compiler error "use of member as default parameter requires static member"

class BinarySearchTree{
TreeNode* root;
...
int TreeMinimumValue(TreeNode* x=root) //this does not compile
{
TreeNode* currentPtr = x;
while(currentPtr->left != NULL)
currentPtr = currentPtr->left;
return currentPtr->key;
}
}//end class BinarySearchTree





I think i don't want to use a static member(as hinted by the compiler). And so i had to use the following code, which compiles, and works, but seems weird, any suggestions on how to improve (or if this is the best way to code this) would be great.



class BinarySearchTree{
TreeNode* root;
...
int TreeMinimumValue(TreeNode* x=NULL)//1. default value is NULL
{
if(x==NULL) x=root; //2. check if NULL, use root

TreeNode* currentPtr = x;
while(currentPtr->left != NULL)
currentPtr = currentPtr->left;
return currentPtr->key;
}
}//end class BinarySearchTree




thanks
Apr 16, 2011 at 2:56am
Does this work for you?
1
2
int TreeMinimumValue(TreeNode* x); //no default value
int TreeMinimumValue() { return TreeMinimumValue(root); }
Apr 16, 2011 at 3:00am
yeah, thanks Mathhead... i did try that also, and it worked fine. i forgot to mention that.

however, i really am trying hard to use the default argument values (sort of as an exercise), and would like to learn if what i've coded is bad style or wrong in some way.

Topic archived. No new replies allowed.