Constructor & default arguments

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • The Directive

    Constructor & default arguments

    This code will not compiled:

    In main function:

    Dot temp = new Dot( *(new Point( 5, 5 )) );

    In Dot class:

    //Constructor with default arguments.
    Dot::Dot( Point& point= *(new Point( 5, 5 )),
    Color& color = *(new Color( 100, 125, 106 )) )
    {
    }

    It complains that:

    Main.cpp
    [Warning] In function `int main(int, char**)':
    Main.cpp
    no matching function for call to `Dot::Dot(Point &)'
    Dot.h
    candidates are: Dot::Dot(Dot&)
    Dot.h
    Dot::Dot(Point& , Color&)
    Main.o(.text+0x 84)
    [Warning] In function `main':
    [Linker error] undefined reference to `Dot::Dot()'

    I'm using DEV-C++ and gcc.

    Why doesn't it find the matching constructor:

    Dot::Dot(Point& , Color&)

    --The Directive
  • Ron Natalie

    #2
    Re: Constructor & default arguments


    "The Directive" <the_directive@ hotmail.com> wrote in message news:8477bc58.0 401141408.181ce fb9@posting.goo gle.com...[color=blue]
    > This code will not compiled:
    >
    > In main function:
    >
    > Dot temp = new Dot( *(new Point( 5, 5 )) );
    >
    > In Dot class:
    >
    > //Constructor with default arguments.
    > Dot::Dot( Point& point= *(new Point( 5, 5 )),
    > Color& color = *(new Color( 100, 125, 106 )) )
    > {
    > }
    >[/color]

    I can't tell for sure with the snippet you've provided, but I suspect that at the point
    where the "new Dot(..." is executed you haven't provided the default arguments yet.
    The declaration with the defaulted arguments must be seen before the place it is used.
    For example:

    void func(int);

    func();

    void func(int x = 3) { }

    won't compile because at the point func() is called, it hasn't seen the default values for
    the arguments.

    Comment

    • Kevin Saff

      #3
      Re: Constructor &amp; default arguments


      "The Directive" <the_directive@ hotmail.com> wrote in message
      news:8477bc58.0 401141408.181ce fb9@posting.goo gle.com...[color=blue]
      > This code will not compiled:
      >
      > In main function:
      >
      > Dot temp = new Dot( *(new Point( 5, 5 )) );[/color]

      temp is of type "Dot", but you are trying to assign a Dot* (which is what
      "new Dot" returns) to it.

      Instead, the correct way to call a constructor might be:

      Dot temp (*new Point (5, 5));

      Code involving "*new" usually leads to memory leaks! You will need to
      forget many Java habits. Do you have a decent C++ book?
      [color=blue]
      >
      > In Dot class:
      >
      > // Constructor with default arguments.
      > Dot::Dot( Point& point= *(new Point( 5, 5 )),
      > Color& color = *(new Color( 100, 125, 106 )) )
      > {
      > }[/color]

      First of all, you can only define default arguments in the function
      declaration. (inside your class for a constructor).

      Also, you're using *new again. In C++ you never use "new" without later
      using "delete", since C++ does not have garbage collection. Since you don't
      actually store the pointers here, you can't delete them, and you will get a
      memory leak any time you use the default arguments.
      [color=blue]
      >
      > It complains that:
      >
      > Main.cpp
      > [Warning] In function `int main(int, char**)':
      > Main.cpp
      > no matching function for call to `Dot::Dot(Point &)'
      > Dot.h
      > candidates are: Dot::Dot(Dot&)
      > Dot.h
      > Dot::Dot(Point& , Color&)
      > Main.o(.text+0x 84)
      > [Warning] In function `main':
      > [Linker error] undefined reference to `Dot::Dot()'
      >
      > I'm using DEV-C++ and gcc.
      >
      > Why doesn't it find the matching constructor:
      >
      > Dot::Dot(Point& , Color&)[/color]

      It's hard to tell without more complete code.
      [color=blue]
      >
      > --The Directive[/color]

      HTH
      --
      KCS


      Comment

      • Peter Koch Larsen

        #4
        Re: Constructor &amp; default arguments


        "The Directive" <the_directive@ hotmail.com> skrev i en meddelelse
        news:8477bc58.0 401141408.181ce fb9@posting.goo gle.com...[color=blue]
        > This code will not compiled:
        >
        > In main function:
        >
        > Dot temp = new Dot( *(new Point( 5, 5 )) );
        >
        > In Dot class:
        >
        > //Constructor with default arguments.
        > Dot::Dot( Point& point= *(new Point( 5, 5 )),
        > Color& color = *(new Color( 100, 125, 106 )) )
        > {
        > }
        >
        > It complains that:
        >
        > Main.cpp
        > [Warning] In function `int main(int, char**)':
        > Main.cpp
        > no matching function for call to `Dot::Dot(Point &)'
        > Dot.h
        > candidates are: Dot::Dot(Dot&)
        > Dot.h
        > Dot::Dot(Point& , Color&)
        > Main.o(.text+0x 84)
        > [Warning] In function `main':
        > [Linker error] undefined reference to `Dot::Dot()'
        >
        > I'm using DEV-C++ and gcc.
        >
        > Why doesn't it find the matching constructor:
        >
        > Dot::Dot(Point& , Color&)
        >
        > --The Directive[/color]

        That code is just so shockfull of errors, I must recommend that you

        a) forget about Java when coding C++.
        b) learn about C++ - in particular when to use new and const correctness.

        One link for you: http://www.parashift.com/C++-faq-lite

        Kind regards
        Peter



        Comment

        • Robert Paul Clark

          #5
          Re: Constructor &amp; default arguments

          Dot *temp = ...;
          or
          Dot temp( *(new ...;

          The Directive wrote:[color=blue]
          > This code will not compiled:
          >
          > In main function:
          >
          > Dot temp = new Dot( *(new Point( 5, 5 )) );
          >
          > In Dot class:
          >
          > //Constructor with default arguments.
          > Dot::Dot( Point& point= *(new Point( 5, 5 )),
          > Color& color = *(new Color( 100, 125, 106 )) )
          > {
          > }
          >
          > It complains that:
          >
          > Main.cpp
          > [Warning] In function `int main(int, char**)':
          > Main.cpp
          > no matching function for call to `Dot::Dot(Point &)'
          > Dot.h
          > candidates are: Dot::Dot(Dot&)
          > Dot.h
          > Dot::Dot(Point& , Color&)
          > Main.o(.text+0x 84)
          > [Warning] In function `main':
          > [Linker error] undefined reference to `Dot::Dot()'
          >
          > I'm using DEV-C++ and gcc.
          >
          > Why doesn't it find the matching constructor:
          >
          > Dot::Dot(Point& , Color&)
          >
          > --The Directive[/color]

          Comment

          • Kevin Goodsell

            #6
            Re: Constructor &amp; default arguments

            Robert Paul Clark wrote:
            <top-posted stuff>

            You've posted a whole bunch of top-posted messages now. Could you please
            stop that? It's very irritating.

            -Kevin
            --
            My email address is valid, but changes periodically.
            To contact me please use the address from a recent posting.

            Comment

            • The Directive

              #7
              Re: Constructor &amp; default arguments

              [Snipped]
              [color=blue]
              > I can't tell for sure with the snippet you've provided, but I suspect that at the point
              > where the "new Dot(..." is executed you haven't provided the default arguments yet.
              > The declaration with the defaulted arguments must be seen before the place it is used.
              > For example:
              >
              > void func(int);
              >
              > func();
              >
              > void func(int x = 3) { }
              >
              > won't compile because at the point func() is called, it hasn't seen the default values for
              > the arguments.[/color]

              Ron,

              You hit the nail on the head.

              --The Directive

              Comment

              • The Directive

                #8
                Re: Constructor &amp; default arguments

                "Kevin Saff" <google.com@kev in.saff.net> wrote in message news:<HrI8D9.Mu 6@news.boeing.c om>...[color=blue]
                > "The Directive" <the_directive@ hotmail.com> wrote in message
                > news:8477bc58.0 401141408.181ce fb9@posting.goo gle.com...[color=green]
                > > This code will not compiled:
                > >
                > > In main function:
                > >
                > > Dot temp = new Dot( *(new Point( 5, 5 )) );[/color]
                >
                > temp is of type "Dot", but you are trying to assign a Dot* (which is what
                > "new Dot" returns) to it.[/color]

                This was a typo when posting the code.
                [color=blue]
                > Instead, the correct way to call a constructor might be:
                >
                > Dot temp (*new Point (5, 5));
                >
                > Code involving "*new" usually leads to memory leaks! You will need to
                > forget many Java habits. Do you have a decent C++ book?
                >[color=green]
                > >
                > > In Dot class:
                > >
                > > // Constructor with default arguments.
                > > Dot::Dot( Point& point= *(new Point( 5, 5 )),
                > > Color& color = *(new Color( 100, 125, 106 )) )
                > > {
                > > }[/color]
                >
                > First of all, you can only define default arguments in the function
                > declaration. (inside your class for a constructor).[/color]

                Your statement is incorrect or I don't comprehend what you're stating:

                #include <iostream>
                #include <stdlib.h>

                using namespace std;

                class base
                {
                public:

                void function (int);
                };

                void base::function (int temp = 4)
                {
                cout << temp << "\n";
                }

                int main(int argc, char *argv[])
                {
                base temp;

                temp.function() ;
                temp.function(7 );

                system("PAUSE") ;
                return 0;
                }

                The code compiles, runs, and does what it's suppose to do.

                However, the following version of the previous program doesn't work:

                #include <iostream>
                #include <stdlib.h>

                using namespace std;

                class base
                {
                public:

                void function (int);
                };

                int main(int argc, char *argv[])
                {
                base temp;

                temp.function() ;
                temp.function(7 );

                system("PAUSE") ;
                return 0;
                }

                void base::function (int temp = 4)
                {
                cout << temp << "\n";
                }

                which surprises me that the compiler (or the C++ standard) is
                not smart enough to compile this version of the program.
                [color=blue]
                > Also, you're using *new again. In C++ you never use "new" without later
                > using "delete", since C++ does not have garbage collection. Since you don't
                > actually store the pointers here, you can't delete them, and you will get a
                > memory leak any time you use the default arguments.[/color]

                When I post code, I usually only post a portion or fragments of the
                true code to make it easy to illustrate my question.

                --The Directive

                Comment

                • The Directive

                  #9
                  Re: Constructor &amp; default arguments

                  [Snipped]
                  [color=blue]
                  > That code is just so shockfull of errors, I must recommend that you
                  >
                  > a) forget about Java when coding C++.
                  > b) learn about C++ - in particular when to use new and const correctness.[/color]

                  I did learn about C++. I'm now putting into practice what I've
                  learned. That's part of learning also. Writing c++ sample code
                  clarifies my C++ knowlege and expands it.

                  I understood people responses about the pointer typo, new/delete
                  usage, and default arguments. What do you specifically mean by "const
                  correctness."?
                  [color=blue]
                  > One link for you: http://www.parashift.com/C++-faq-lite
                  >
                  > Kind regards
                  > Peter[/color]

                  --The Directive

                  Comment

                  • Karl Heinz Buchegger

                    #10
                    Re: Constructor &amp; default arguments

                    The Directive wrote:[color=blue]
                    >
                    > First of all, you can only define default arguments in the function[color=green]
                    > > declaration. (inside your class for a constructor).[/color]
                    >
                    > Your statement is incorrect or I don't comprehend what you're stating:[/color]

                    Not at all:

                    8.3.6 Default arguments

                    3 A default argument expression shall be specified only in the
                    parameter-declaration-clause of a function declaration or in
                    a template-parameter.

                    [snip example 1]
                    [color=blue]
                    >
                    > The code compiles, runs, and does what it's suppose to do.[/color]

                    .... but is still illegal.

                    [snip example 2]
                    [color=blue]
                    > which surprises me that the compiler (or the C++ standard) is
                    > not smart enough to compile this version of the program.[/color]

                    The compiler reads the source code from top to bottom. It never
                    steps back and corrects previously compiled code.

                    In

                    class base
                    {
                    public:
                    void function(int);
                    };

                    int main()
                    {
                    base temp;

                    temp.function() ;

                    the compiler expects you to provide an argument to function(). This
                    is what the declaration has told the compiler.
                    [color=blue]
                    >
                    > When I post code, I usually only post a portion or fragments of the
                    > true code to make it easy to illustrate my question.
                    >[/color]

                    Then prepare that people will correct mistakes in your posting which
                    are not there in your true source code. If you do that often enough
                    (posting not the true code), people finally will stop correcting
                    your programs, since it is a waste of time to correct errors and
                    afterwards you are saying: it was only a typo in posting.

                    --
                    Karl Heinz Buchegger
                    kbuchegg@gascad .at

                    Comment

                    • Karl Heinz Buchegger

                      #11
                      Re: Constructor &amp; default arguments

                      Karl Heinz Buchegger wrote:[color=blue]
                      >[/color]
                      [color=blue][color=green]
                      > >
                      > > When I post code, I usually only post a portion or fragments of the
                      > > true code to make it easy to illustrate my question.
                      > >[/color]
                      >
                      > Then prepare that people will correct mistakes in your posting which
                      > are not there in your true source code. If you do that often enough
                      > (posting not the true code), people finally will stop correcting
                      > your programs, since it is a waste of time to correct errors and
                      > afterwards you are saying: it was only a typo in posting.
                      >[/color]

                      Forget that last paragraph. I misread what you typed. You post
                      real code (using cut&paste), that's fine.

                      But still: using *new usually indicates a problem.

                      --
                      Karl Heinz Buchegger
                      kbuchegg@gascad .at

                      Comment

                      • Peter Koch Larsen

                        #12
                        Re: Constructor &amp; default arguments


                        "The Directive" <the_directive@ hotmail.com> skrev i en meddelelse
                        news:8477bc58.0 401151110.3a255 6f4@posting.goo gle.com...[color=blue]
                        > [Snipped]
                        >[color=green]
                        > > That code is just so shockfull of errors, I must recommend that you
                        > >
                        > > a) forget about Java when coding C++.
                        > > b) learn about C++ - in particular when to use new and const[/color][/color]
                        correctness.[color=blue]
                        >
                        > I did learn about C++. I'm now putting into practice what I've
                        > learned. That's part of learning also. Writing c++ sample code
                        > clarifies my C++ knowlege and expands it.
                        >
                        > I understood people responses about the pointer typo, new/delete
                        > usage, and default arguments. What do you specifically mean by "const
                        > correctness."?
                        >[color=green]
                        > > One link for you: http://www.parashift.com/C++-faq-lite
                        > >
                        > > Kind regards
                        > > Peter[/color]
                        >
                        > --The Directive[/color]

                        Ok - I'll bite. Here is your original code:

                        [color=blue]
                        > Dot temp = new Dot( *(new Point( 5, 5 )) );[/color]

                        Your first confusion (Java-inspired):

                        In C++
                        a) whenever you use new to create some variable (e.g. an object), you must
                        use delete to reclaim the space allocated. C++ does not have
                        garbage-collection, so you are leaking memory.
                        b) new does not return an object, but a pointer to one. Thus, the code above
                        would not compile (you can't normally assign a pointer to an object to an
                        object). Probably the code above should be written as:

                        Dot temp = Dot(Point( 5, 5));

                        or more succintly:
                        Dot temp(Point( 5, 5));

                        However, with the definition of the Dot-constructer being as it is, this
                        would not compile. Let's move on:
                        [color=blue]
                        >
                        > In Dot class:
                        >
                        > //Constructor with default arguments.
                        > Dot::Dot( Point& point= *(new Point( 5, 5 )),
                        > Color& color = *(new Color( 100, 125, 106 )) )[/color]

                        Here you do again use new to create an object. new is a relatively slow
                        operation (compared to no new), and thus the call is less than optimal in
                        speed. This is neglectible compared to the memory leak, of course. The
                        proper constructor should probably be:

                        Dot::Dot(Point point = Point( 5,5),Color color = Color(100,125,1 06))

                        which is better except for a perhaps inefficient passing of arguments. And
                        this is where const correctness comes into play. If you can, you should
                        always strive to pass arguments by value (as above) or by const reference
                        (prefer const reference if you can - except for the simplest types (built
                        ins)). This gives you the following interface:

                        Dot::Dot(Point const& point = Point( 5,5),Color const& color =
                        Color(100,125,1 06))

                        In a constructor you should always strive to allow all parameters to be
                        temporary - which means pass-by value (Point point) or by const reference
                        (Point const& point).

                        When you see an assignment x = y; you would not normally expect y to change,
                        would you? And yet, this is precisely what could happen in your
                        Dot-constructor.

                        Kind regards
                        Peter


                        Comment

                        Working...