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?

Recommended Answers

All 4 Replies

Here is an explaination of default parameter values. That link talks about exactly what you are trying to do.

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.

solved, thank you for your help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.