Newbie: constructor of the base class not being found?

A

A. Farber

Hi,

could someone please provide a gentle explanation
(for a C-programmer trying C++) why doesn't the following
code compile? (I've tried MS Visual C++ 6 on Win as well).

Also what is the mentioned Circle::Circle (const Circle &)
for? I remember reading something about it before, but
can't find that information now...

Thank you
Alex

bolinux12:afarber {92} cat shapes.cpp
#include <iostream>
using namespace std;

class Shape {
int x, y;
public:
Shape(int x, int y) { }
};

class Circle: public Shape {
};

int main() {
Circle c(100, 120);
return 0;
}

bolinux12:afarber {93} g++296 -o shapes shapes.cpp
shapes.cpp:11: base `Shape' with only non-default constructor in class
without a constructor
shapes.cpp: In function `int main ()':
shapes.cpp:14: no matching function for call to `Circle::Circle (int,
int)'
shapes.cpp:11: candidates are: Circle::Circle (const Circle &)
 
T

TheFerryman

Hi,

could someone please provide a gentle explanation
(for a C-programmer trying C++) why doesn't the following
code compile? (I've tried MS Visual C++ 6 on Win as well).

Also what is the mentioned Circle::Circle (const Circle &)
for? I remember reading something about it before, but
can't find that information now...

Thank you
Alex

bolinux12:afarber {92} cat shapes.cpp
#include <iostream>
using namespace std;

class Shape {
int x, y;
public:
Shape(int x, int y) { }
};

class Circle: public Shape {
};

int main() {
Circle c(100, 120);
return 0;
}

bolinux12:afarber {93} g++296 -o shapes shapes.cpp
shapes.cpp:11: base `Shape' with only non-default constructor in class
without a constructor
shapes.cpp: In function `int main ()':
shapes.cpp:14: no matching function for call to `Circle::Circle (int,
int)'
shapes.cpp:11: candidates are: Circle::Circle (const Circle &)

The Circle inherits from Shape, which only has constructor that
accepts two parameters. When you attempt to instantiate a Circle, it,
in turn must instantiate a Shape object. It cannot do this because
there is no default constructor for Shape. Instead you must do
something like this


class Circle: public Shape
{
public:
Circle(int x, int y):Shape(x,y){}
};

Alternatively, you can declare an alternative constructor for Shape.
Like this:

class Shape
{
int x;
int y;

public:
Shape(int a,int a):x(a), y(b){} //make sure the variables are
initialized
Shape():x(0), y(0){} //make sure variables are initialized with a
value
};
 
U

Unforgiven

A. Farber said:
Hi,

could someone please provide a gentle explanation
(for a C-programmer trying C++) why doesn't the following
code compile? (I've tried MS Visual C++ 6 on Win as well).

Also what is the mentioned Circle::Circle (const Circle &)
for? I remember reading something about it before, but
can't find that information now...

Thank you
Alex

bolinux12:afarber {92} cat shapes.cpp
#include <iostream>
using namespace std;

class Shape {
int x, y;
public:
Shape(int x, int y) { }
};

class Circle: public Shape {
};

int main() {
Circle c(100, 120);
return 0;
}

bolinux12:afarber {93} g++296 -o shapes shapes.cpp
shapes.cpp:11: base `Shape' with only non-default constructor in class
without a constructor
shapes.cpp: In function `int main ()':
shapes.cpp:14: no matching function for call to `Circle::Circle (int,
int)'
shapes.cpp:11: candidates are: Circle::Circle (const Circle &)

When you create a class that does not explicitly have a constructor the C++
language still creates some: a default constructor (with no arguments) and a
copy constructor. As soon as you define a constructor yourself, these don't
get created anymore. So your class Shape has a constructor
Shape::Shape(int,int), but not a constructor Shape::Shape().

In class Circle you define no constructor of your own, so the default
constructor is generated. This constructor Circle::Circle() wants to call
the default base class constructor Shape::Shape(), but as I said that
doesn't exist. This explains the first error.

The other errors are because you didn't give Circle a constructor that takes
two ints, so it doesn't have one (even though the base class does). The
Circle::Circle(const Circle &) is the autogenerated copy constructor, which
it sees as the closest match to your constructor call.

To make your code work you need to define Circle as follows:

class Circle : public Shape
{
public:
Circle(int x, int y) : Shape(x, y) { }
};

Now it'll compile.
 

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
474,163
Messages
2,570,897
Members
47,434
Latest member
TobiasLoan

Latest Threads

Top