A problem with Constructors...

A

aravandor

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

Alf P. Steinbach

* 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>.
 
S

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() { }

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
 
S

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() : 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
 
A

Alf P. Steinbach

* 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.
 
S

Srini

* Alf
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
 
S

Srini

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_.
 
R

Rolf Magnus

Srini said:
* Alf


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

Alf P. Steinbach

* Rolf Magnus:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top