default arguments in member function

Discussion in 'C++' started by Neelesh Bodas, Dec 9, 2005.

  1. 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.
    Neelesh Bodas, Dec 9, 2005
    #1
    1. Advertising

  2. Neelesh Bodas

    mlimber Guest

    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
    mlimber, Dec 9, 2005
    #2
    1. Advertising

  3. 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
    Victor Bazarov, Dec 9, 2005
    #3
  4. 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) ?
    Neelesh Bodas, Dec 9, 2005
    #4
  5. 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.
    Neelesh Bodas, Dec 9, 2005
    #5
  6. 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
    Victor Bazarov, Dec 9, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Edward Diener
    Replies:
    14
    Views:
    4,886
    Josiah Carlson
    Apr 6, 2004
  2. tutmann
    Replies:
    4
    Views:
    429
  3. jmborr
    Replies:
    1
    Views:
    400
    Stargaming
    Nov 3, 2007
  4. Hamish
    Replies:
    3
    Views:
    554
    Alf P. Steinbach
    Jan 25, 2008
  5. Hicham Mouline
    Replies:
    3
    Views:
    388
    Hicham Mouline
    Sep 16, 2009
Loading...

Share This Page