views:

140

answers:

3

A schematic of my problem...

class A
{
public:
    // etc.
protected:
    uint num;
};

class B : public A
{
public: 
    void foo(uint x = num); //bad
};

gives this error:

error: invalid use of non-static data member ‘A::num’
error: from this location

Why does this happen, and what can I do to work around this?

+4  A: 

I suspect this happens (based on the complaint about non-staticness) because there is no this pointer for it to use to know which instance of B it should get num from.

The Microsoft compiler (at least) allows you to specify an expression, but not a non-static member. From MSDN:

The expressions used for default arguments are often constant expressions, but this is not a requirement. The expression can combine functions that are visible in the current scope, constant expressions, and global variables. The expression cannot contain local variables or non-static class-member variables.

Work-arounds for this are numerous and others have pointed out a few. Here's one more which you may or may not like:

void foo(uint* x = NULL) {
  uint y = (x == NULL ? num : *x);
  // use y...
}
jeffamaphone
the workaround I used was assigning the default to UINT_MAX instead of NULL, but that caused conflicts elsewhere. i just overloaded foo() instead. thanks for the "this" explanation.
johndashen
@jeffamaphone: No suspicion needed, its simply illegal to refer to non-static members in default arguments (except for pointers to members and class member access expressions).
Georg Fritzsche
Not just Microsoft. The standard allows expressions, but it forbids non-static members in 8.3.6/9: "Similarly, a nonstatic member shall not be used in a default argument expression, even if it is not evaluated, unless it appears as the id-expression of a class member access expression (5.2.5) or unless it is used to form a pointer to member (5.3.1)."
Rob Kennedy
Oh good. I didn't have time to go try to find the standard. :)
jeffamaphone
Why not edit the information in then?
Georg Fritzsche
A: 

you can create 2 foos

foo() //use num internally

foo(int x) //use x

Chris H
+2  A: 

So heres what you do to avoid the problem.

class A
{
public:
    // etc.
protected:
    uint num;
};

class B : public A
{
public: 
    void foo(uint x);
    void foo() { foo( num ); }
};
Michael Anderson
thanks... this is the obvious solution but for some reason it skipped my mind. (the //bad comment in the solution is no longer needed, it's just from my question code)
johndashen