2 class / no default constructor available

S

stephane

I can't make this work it says I dont have a default constructor available.

Can someone tell me what's wrong with this please?

#include <iostream>
using namespace std;

#include "points.h"

int main ()

{


Trectangle rec1(2,5,10,6);

Trectangle rec2(rec1);



return 0;
}


// points.h

#ifndef POINTS_H
#define POINTS_H

#include <iostream>
using namespace std;

class Tpoints{

public:

Tpoints(double x,double y):m_x(x),m_y(y)
{
cout<<"constr points 1"<<endl;


}

Tpoints(Tpoints& p)
{
m_x = p.m_x;
m_y = p.m_y;

cout<<"constr copie Tpoints"<<endl;
}


private:

double m_x;
double m_y;
};


class Trectangle{

public:



Trectangle(double x,double y, double larg, double haut)
: m_hautGauche(x,y), m_basDroite(x+larg, y+haut){cout<<"constr rec
1"<<endl;}

Trectangle(Trectangle& r)
{
cout<<"constr copie rec"<<endl;

m_hautGauche = r.m_hautGauche;
m_basDroite = r.m_basDroite;
}
// no appropriate default constructor available




private:

Tpoints m_hautGauche;
Tpoints m_basDroite;
};



#endif
 
D

Dietmar Kuehl

stephane said:
Trectangle(double x,double y, double larg, double haut)
: m_hautGauche(x,y), m_basDroite(x+larg, y+haut){cout<<"constr rec
1"<<endl;}

Trectangle(Trectangle& r)
{
m_hautGauche = r.m_hautGauche;
m_basDroite = r.m_basDroite;
}

You need to use a member initializer list for this constructor, too:

| Trectangle(Trectangle const& r):
| m_hautGauche(r.hautGauche),
| m_basDrote(r.m_basDrote)
| { /* ... */ }

I also added a 'const' to the parameter as you don't modify the
object. However, I haven't verified that 'TPoint's copy constructor
is correct declared. BTW, note that you don't need to provide this
copy constructor: if you don't mention it, the compiler will
automatically create an appropriate version in this case - it knows
how to copy the members...
 
K

Karl Heinz Buchegger

stephane said:
I can't make this work it says I dont have a default constructor available.

Can someone tell me what's wrong with this please?

Exactly as the compiler indicates: There is no default constructor
( = one that takes no arguments ) for class Tpoints

So why does the compiler need one?
Easy: Because in class Trectangle, there are 2 members of type Tpoints.
Also class Trectangle has a copy constructor. But in this copy constructor
you do not specify how the Tpoints members should be constructed. Thus the
compiler tries to use the default constructor to do that. But there is none.


Trectangle( const Trectangle& r ) : m_hautGauche( r.m_hautGauche ),
m_basDroite( r.m_basDroite )
{
cout << "const copie rec" << endl;
}

Now the compiler is instructed to use the copy constructor for the Tpoints
members.
 
S

Stephane Vollet

If I write Trectangle(Trectangle&
r):m_hautGauche(r.m_hautGauche),m_basDroite(r.m_basDroite)

It compiles ok, but if I add "const" like this

Trectangle(const Trectangle&
r):m_hautGauche(r.m_hautGauche),m_basDroite(r.m_basDroite)

it says there isn't any copy constructor! Why's that?


#ifndef POINTS_H
#define POINTS_H

#include <iostream>
using namespace std;

class Tpoints{

public:

Tpoints(double x,double y):m_x(x),m_y(y)
{
cout<<"constr points 1"<<endl;
}

Tpoints(Tpoints& p)
{
m_x = p.m_x;
m_y = p.m_y;

cout<<"constr copie Tpoints"<<endl;
}

private:

double m_x;
double m_y;
};

class Trectangle{

public:

Trectangle(double x,double y, double larg, double haut)
: m_hautGauche(x,y), m_basDroite(x+larg, y+haut){cout<<"constr rec
1"<<endl;}

Trectangle(Trectangle&
r):m_hautGauche(r.m_hautGauche),m_basDroite(r.m_basDroite)
{
cout<<"constr copie rec"<<endl;

}
// no appropriate default constructor available

private:

Tpoints m_hautGauche;
Tpoints m_basDroite;
};

#endif
 
V

Victor Bazarov

Stephane said:
If I write Trectangle(Trectangle&
r):m_hautGauche(r.m_hautGauche),m_basDroite(r.m_basDroite)

It compiles ok, but if I add "const" like this

Trectangle(const Trectangle&
r):m_hautGauche(r.m_hautGauche),m_basDroite(r.m_basDroite)

it says there isn't any copy constructor! Why's that?
[...]

Tpoints(Tpoints& p)
^^^^^^^^^^
Because of this. Shouldn't this be 'const' as well?
{
m_x = p.m_x;
m_y = p.m_y;

Prefer initialisation over assignment. See FAQ.
cout<<"constr copie Tpoints"<<endl;
}
[...]
 

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,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top