the title describes the problem. here's how the code looks like:

#include <cstdio>

class example
{
	public:
	class node
	{
		public:
		int value;
		node *left, *right;
	};

	node *root;

	example (int a)
	{
		root=new node;
		root->value=a;
	}

// HERE'S THE PROBLEM:
	void function (node *variable=root)
	{

	}
};

int main()
{

}

g++ returns:

p.cpp:13: error: invalid use of non-static data member 'przyklad::root'
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?

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

void function (node *variable=this->root)

i get

p.cpp:21: error: 'this' may not be used in this context

a correct code would be highly appreciated

>> 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:

void f(int a)
{
      int b=4;
      void g(int c=a); // Local variable "a" cannot be used here
      void h(int d=b); // Local variable "b" cannot be used here
}

>>a correct code would be highly appreciated
The only correct way to do that is to make the local variable static.

This article has been dead for over six months. Start a new discussion instead.