473,320 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

A problem with Constructors...

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?

Jul 23 '05 #1
8 1323
* 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?
Jul 23 '05 #2
> 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

Jul 23 '05 #3
> 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

Jul 23 '05 #4
* 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?
Jul 23 '05 #5
* 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

Jul 23 '05 #6
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?

Jul 23 '05 #7
> 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_.

Jul 23 '05 #8
* 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?
Jul 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Beach Potato | last post by:
I guess I've been out of C++ for a while, since right now I don't seem to get a simple solution for overriding inherited constrictors. What worked in Borland C++ & Pascal (and Java, if I remember...
42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
6
by: Doug | last post by:
I have a public abstract class that I want to inherit from in two other classes. My public abstract one has a constructor with several parameters. For some reason I cannot get to that constructor...
1
by: E. Robert Tisdale | last post by:
Please find attached a copy of the "Shape class" example from Bjarne Stroustrup, "The C++ Programming Language: Third Edition", Chapter 2: A Tour of C++, Section 6: Object-Oriented Programming,...
3
by: craigkenisston | last post by:
Hi, I have a class with 3 constructors, one takes no arguments, other take an string and other an xml document. I want to derive another class of it, I don't need a different constructor so I...
10
by: Kevin Buchan | last post by:
I searched the news group and could not find an answer to this question, so I'll go ahead and post it. Let's say I have a class A with a couple different constructors... nothin' special. Now,...
12
by: Oleg Subachev | last post by:
I am moving from Delphi to C# and hve encountered the problem: I have the following classes and form Load event handler: public class class1 { public string S; public class1( string aS ) {...
22
by: Peter Morris [Droopy eyes software] | last post by:
Look at these two classes public class Test { public readonly string Name; public Test(string name)
4
by: Adam | last post by:
Okay, so I know this will come off as a stupid question...but I am going to ask it anyways... I know you are not allowed to have constructors defined in an interface, but why not? I really...
6
by: Peng Yu | last post by:
Hi, I want B has all the constructors that A has. Obviously, the code below would not work. I could define a corresponding B's constructor for each A's constructor. But if A has many...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.