A problem with Constructors...

Discussion in 'C++' started by aravandor, Jun 30, 2005.

  1. aravandor

    aravandor Guest

    I'm a C++ beginner coming from Java and VB.Net

    I know that this is an elementary question, but I'm stumped. I'm
    trying to make a coordinate class to store 2 Dimensional points and a
    Rectangle class that is defined by two of these coords.

    I'm using the mingw32 compiler with Bloodshed's DevC++ IDE. When I
    compile this code, I keep getting the following errors from the mingw32
    compiler:

    no matching function for call to `coord::coord ()'
    candidates are: coord::coord(int, int)

    Here's the code:

    class coord {
    public:
    int x;
    int y;

    coord(int a, int b) {
    x = a;
    y = b;
    }

    coord(const coord& rhs) {
    x = rhs.x;
    y = rhs.y;
    }
    };

    class rectangle {
    public:
    coord c1;
    coord c2;
    int color;

    rectangle (coord a, coord b, int c = 0) {
    c1 = a;
    c2 = b;
    color = c;
    }

    rectangle(int x1, int y1, int x2, int y2, int c = 0) {
    c1 = coord(x1,y1);
    c2 = coord(x2,y2);
    color = c;
    }

    void draw() {
    rectfill(screen, c1.x, c1.y, c2.x, c2.y, color);
    }

    };

    I know I'm making a stupid mistake here. Can anyone help me out?
    aravandor, Jun 30, 2005
    #1
    1. Advertising

  2. * aravandor:
    > I'm a C++ beginner coming from Java and VB.Net


    Oops.


    > I know that this is an elementary question, but I'm stumped. I'm
    > trying to make a coordinate class to store 2 Dimensional points and a
    > Rectangle class that is defined by two of these coords.
    >
    > I'm using the mingw32 compiler with Bloodshed's DevC++ IDE. When I
    > compile this code, I keep getting the following errors from the mingw32
    > compiler:
    >
    > no matching function for call to `coord::coord ()'
    > candidates are: coord::coord(int, int)


    The error message says exactly what the problem is.


    > Here's the code:
    >
    > class coord {
    > public:
    > int x;
    > int y;
    >
    > coord(int a, int b) {


    Preferably use a constructor initializer list, that's a good
    habit.

    > x = a;
    > y = b;
    > }
    >
    > coord(const coord& rhs) {
    > x = rhs.x;
    > y = rhs.y;
    > }


    The compiler generates this copy constructor for you if you don't
    define it.


    > };


    I.e., this is your class:

    struct coord
    {
    int x;
    int y;
    coord( int a, int b ): x( a ), y( b ) {}
    };


    >
    > class rectangle {
    > public:
    > coord c1;
    > coord c2;
    > int color;
    >
    > rectangle (coord a, coord b, int c = 0) {


    I'd pass those coordinates by ref to const.

    Use a constructor initializer list (example shown above).

    There's no default constructor for class 'coord'.


    > c1 = a;
    > c2 = b;
    > color = c;
    > }
    >
    > rectangle(int x1, int y1, int x2, int y2, int c = 0) {
    > c1 = coord(x1,y1);
    > c2 = coord(x2,y2);
    > color = c;
    > }
    >
    > void draw() {
    > rectfill(screen, c1.x, c1.y, c2.x, c2.y, color);
    > }
    >
    > };
    >
    > I know I'm making a stupid mistake here. Can anyone help me out?


    See above explanation, and also
    <url: http://home.no.net/dubjai/win32cpptut/html/w32cpptut_02.html>.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Jun 30, 2005
    #2
    1. Advertising

  3. aravandor

    Srini Guest

    > no matching function for call to `coord::coord ()'
    > candidates are: coord::coord(int, int)


    You have not defined a default constructor. This is required if you are
    creating 'coord' objects without any arguments like...

    coord obj;
    or
    coord objarr[10];

    Either define a seperate default constructor...

    coord::coord() { }

    or give some reasonable default values for the constructor that takes 2
    ints

    explicit coord(int a = 0, int b = 0) : x(a), y(b) { }

    Regards,
    Srini
    Srini, Jun 30, 2005
    #3
  4. aravandor

    Srini Guest

    > no matching function for call to `coord::coord ()'
    > candidates are: coord::coord(int, int)


    You have not defined a default constructor. This is required if you are
    creating 'coord' objects without any arguments like...

    coord obj;
    or
    coord objarr[10];

    Either define a seperate default constructor...

    coord::coord() : x(0), y(0) { }

    or give some reasonable default values for the constructor that takes 2
    ints

    explicit coord(int a = 0, int b = 0) : x(a), y(b) { }

    Regards,
    Srini
    Srini, Jun 30, 2005
    #4
  5. * Srini:
    > > no matching function for call to `coord::coord ()'
    > > candidates are: coord::coord(int, int)

    >
    > You have not defined a default constructor. This is required if you are
    > creating 'coord' objects without any arguments like...
    >
    > coord obj;
    > or
    > coord objarr[10];
    >
    > Either define a seperate default constructor...
    >
    > coord::coord() { }


    This creates an uninitialized coord, with arbitrary values.


    > or give some reasonable default values for the constructor that takes 2
    > ints
    >
    > explicit coord(int a = 0, int b = 0) : x(a), y(b) { }


    This defines a conversion from 'int' to 'coord' as using the integer
    value as x-coordinate, which will probably be surprising even if
    'explicit'.

    A better solution is to use initializer lists, and for vectors of
    coordinates, std::vector, and only add a default constructor if
    required. That way (as a habit) it's not so easy to fall into the
    Java/VB trap of having non-usable instances of objects around. Also,
    it's not so easy to create constructors like the first one above.


    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Jun 30, 2005
    #5
  6. aravandor

    Srini Guest

    * Alf

    > > explicit coord(int a = 0, int b = 0) : x(a), y(b) { }

    >
    > This defines a conversion from 'int' to 'coord' as using the integer
    > value as x-coordinate, which will probably be surprising even if
    > 'explicit'.


    I don't understand how the above will define a conversion fron 'int' to
    'coord' even if 'explicit' is specified. I thought that 'explicit'
    supresses the compiler to apply user-defined conversions automatically.
    Am I missing something?

    Regards,
    Srini
    Srini, Jun 30, 2005
    #6
  7. aravandor

    Srini Guest

    > No. It just means that the conversion needs to be done explicitly (hence the
    > name).
    > However, I fail to see why such a conversion constructor could catch you by
    > surprise. I mean who would try to cast from int to coord and then be
    > surprised that it actually is accepted by the compiler?


    Ya - that's possible. And yes, as you point out, the fact that such a
    cast would work is not of any surprise. The compiler won't do that sort
    of a conversion _automatically_.
    Srini, Jun 30, 2005
    #7
  8. aravandor

    Rolf Magnus Guest

    Srini wrote:

    > * Alf
    >
    >> > explicit coord(int a = 0, int b = 0) : x(a), y(b) { }

    >>
    >> This defines a conversion from 'int' to 'coord' as using the integer
    >> value as x-coordinate, which will probably be surprising even if
    >> 'explicit'.

    >
    > I don't understand how the above will define a conversion fron 'int' to
    > 'coord' even if 'explicit' is specified. I thought that 'explicit'
    > supresses the compiler to apply user-defined conversions automatically.


    No. It just means that the conversion needs to be done explicitly (hence the
    name).
    However, I fail to see why such a conversion constructor could catch you by
    surprise. I mean who would try to cast from int to coord and then be
    surprised that it actually is accepted by the compiler?
    Rolf Magnus, Jun 30, 2005
    #8
  9. * Rolf Magnus:
    > Srini wrote:
    > > * Alf
    > >
    > >> > explicit coord(int a = 0, int b = 0) : x(a), y(b) { }
    > >>
    > >> This defines a conversion from 'int' to 'coord' as using the integer
    > >> value as x-coordinate, which will probably be surprising even if
    > >> 'explicit'.

    > >
    > > I don't understand how the above will define a conversion fron 'int' to
    > > 'coord' even if 'explicit' is specified. I thought that 'explicit'
    > > supresses the compiler to apply user-defined conversions automatically.

    >
    > No. It just means that the conversion needs to be done explicitly (hence the
    > name).
    > However, I fail to see why such a conversion constructor could catch you by
    > surprise. I mean who would try to cast from int to coord and then be
    > surprised that it actually is accepted by the compiler?


    Perhaps folks who don't even see that it defines a conversion, which
    should have been obvious to you as you wrote that reply, having an
    example right in front of your nose.

    Most programmers, unfortunately, work by trial-and-error, not really
    understanding what they do except in a hazy intuitive sort of way; if
    something "works" by adding a word or parenthesis, then they do that.

    There are even some C++ programmers who don't completely grasp the
    mechanisms of C++ object construction.

    Anyway, if a conversion is wanted, then it should be defined clearly as
    a conversion.

    Finally, the person using a conversion might not be the same person
    later maintaining that code.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Jun 30, 2005
    #9
    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. Dave Rudolf
    Replies:
    12
    Views:
    8,250
    Martijn Lievaart
    Feb 6, 2004
  2. Jeremy Smith
    Replies:
    2
    Views:
    574
    Jeremy Smith
    Aug 3, 2006
  3. Jess
    Replies:
    5
    Views:
    583
    Ron Natalie
    Jun 7, 2007
  4. Peng Yu
    Replies:
    5
    Views:
    383
    Juha Nieminen
    Sep 19, 2008
  5. srp113
    Replies:
    3
    Views:
    456
Loading...

Share This Page