By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,419 Members | 1,179 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,419 IT Pros & Developers. It's quick & easy.

default arguments in member function

P: n/a
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.

Dec 9 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Neelesh Bodas wrote:
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

Dec 9 '05 #2

P: n/a
Neelesh Bodas wrote:
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
Dec 9 '05 #3

P: n/a

mlimber wrote:
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
error: a nonstatic member reference must be relative to a specific object


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) ?

Dec 9 '05 #4

P: n/a

Victor Bazarov wrote:

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.

Dec 9 '05 #5

P: n/a
Neelesh Bodas wrote:
Victor Bazarov wrote:
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.


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

V
Dec 9 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.