Mixing default and non-default parameters in Constructor?

Discussion in 'C++' started by desktop, Jun 6, 2007.

  1. desktop

    desktop Guest

    Are there some way to mix default and non-default parameters in a
    constructor:

    class test {

    public:
    test(int i = 45, int j) : pp(j){}
    private:
    int pp;
    int i;
    };

    The above does not work and gives the error:

    main.cpp:144: error: default argument missing for parameter 2 of
    ‘test::test(int, int)’
    make: *** [main.o] Error 1
     
    desktop, Jun 6, 2007
    #1
    1. Advertising

  2. desktop

    red floyd Guest

    desktop wrote:
    > Are there some way to mix default and non-default parameters in a
    > constructor:
    >
    > class test {
    >
    > public:
    > test(int i = 45, int j) : pp(j){}

    test(int j, int i_ = 45) : pp(j), i(i_) { }
    > private:
    > int pp;
    > int i;
    > };
    >
    > The above does not work and gives the error:
    >
    > main.cpp:144: error: default argument missing for parameter 2 of
    > ‘test::test(int, int)’
    > make: *** [main.o] Error 1



    Default parameters must be at the *end* of the parameter list.
    Note also, that you did not initialize your member variable "i".
     
    red floyd, Jun 6, 2007
    #2
    1. Advertising

  3. desktop wrote:
    > Are there some way to mix default and non-default parameters in a
    > constructor:
    >
    > class test {
    >
    > public:
    > test(int i = 45, int j) : pp(j){}
    > private:
    > int pp;
    > int i;
    > };
    >
    > The above does not work and gives the error:
    >
    > main.cpp:144: error: default argument missing for parameter 2 of
    > ‘test::test(int, int)’
    > make: *** [main.o] Error 1


    Yes, but default arguments have to be the last one.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jun 6, 2007
    #3
    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. Aire
    Replies:
    3
    Views:
    468
    Mike Wahler
    Jan 25, 2004
  2. Replies:
    9
    Views:
    966
    Alf P. Steinbach
    Mar 6, 2006
  3. Generic Usenet Account
    Replies:
    10
    Views:
    2,246
  4. Daniel Frechette
    Replies:
    2
    Views:
    185
    Thomas 'PointedEars' Lahn
    Apr 10, 2006
  5. ingoweiss
    Replies:
    4
    Views:
    210
    Julian Turner
    May 12, 2006
Loading...

Share This Page