default arguments in member function

N

Neelesh Bodas

Hello All,
The code -

#include <map>
#include <vector>
using namespace std;

class X {
map<int, vector<int> > foo;
public:
void fun(int x = foo.begin()->first) { } // Error on this line
};

int main()
{
X c;
c.fun();
}

Comeau online says -
error: a nonstatic member reference must be relative to a specific
object
void fun(int x = foo.begin()->first)
^

Can't really understand this. Mostly I am missing something quite
basic. Not sure though.
 
M

mlimber

Neelesh said:
Hello All,
The code -

#include <map>
#include <vector>
using namespace std;

class X {
map<int, vector<int> > foo;
public:
void fun(int x = foo.begin()->first) { } // Error on this line
};

int main()
{
X c;
c.fun();
}

Comeau online says -
error: a nonstatic member reference must be relative to a specific
object
void fun(int x = foo.begin()->first)
^

Can't really understand this. Mostly I am missing something quite
basic. Not sure though.

class X {
map<int, vector<int> > foo;
public:
void fun( int x ) { }
void fun() { fun( foo.begin()->first ); }
};

Cheers! --M
 
V

Victor Bazarov

Neelesh said:
The code -

#include <map>
#include <vector>
using namespace std;

class X {
map<int, vector<int> > foo;
public:
void fun(int x = foo.begin()->first) { } // Error on this line
};

int main()
{
X c;
c.fun();
}

Comeau online says -
error: a nonstatic member reference must be relative to a specific
object
void fun(int x = foo.begin()->first)
^

Can't really understand this. Mostly I am missing something quite
basic. Not sure though.

You're not allowed to use data members as default arguments. The
work-around is simple enough, it's overloading:

class X {
map<int, vector<int> > foo;
public:
void fun(int x) { }
void fun() { fun(foo.begin()->first); }
};

HTH

V
 
N

Neelesh Bodas

mlimber said:
class X {
map<int, vector<int> > foo;
public:
void fun( int x ) { }
void fun() { fun( foo.begin()->first ); }
};

Thanks.Could you please explain the meaning of
Is there a rule that the default argument to a member function cannot
be the return value of another member function (or a rule more general
than that) ?
 
N

Neelesh Bodas

Victor said:
You're not allowed to use data members as default arguments. The
work-around is simple enough, it's overloading:

It seems even member functions are not allowed -

int X::bar();
void X::foo(int x= bar()) { } // Similar Error.
 
V

Victor Bazarov

Neelesh said:
It seems even member functions are not allowed -

int X::bar();
void X::foo(int x= bar()) { } // Similar Error.

Yes, any members (that would require implicit 'this->') are not allowed.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,999
Latest member
MakersCBDGummiesReview

Latest Threads

Top