473,406 Members | 2,217 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,406 software developers and data experts.

2 class / no default constructor available

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
Jul 22 '05 #1
4 3765
stephane wrote:

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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2
stephane wrote:
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...
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 22 '05 #3
If I write Trectangle(Trectangle&
r):m_hautGauche(r.m_hautGauche),m_basDroite(r.m_ba sDroite)

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

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

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_ba sDroite)
{
cout<<"constr copie rec"<<endl;

}
// no appropriate default constructor available

private:

Tpoints m_hautGauche;
Tpoints m_basDroite;
};

#endif
"Dietmar Kuehl" <di***********@yahoo.com> a écrit dans le message de news:
11**********************@z14g2000cwz.googlegroups. com...
stephane wrote:
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...
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 22 '05 #4
Stephane Vollet wrote:
If I write Trectangle(Trectangle&
r):m_hautGauche(r.m_hautGauche),m_basDroite(r.m_ba sDroite)

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

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

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;
}
[...]

Jul 22 '05 #5

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

Similar topics

50
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong...
7
by: Dominique | last post by:
Suppose I have: class A { A(int x, int y); }; and class B: public A {
11
by: Neil Zanella | last post by:
Hello, When an "std::map<int, int> foobar;" statement appears in a C++ program followed by a statement of the form "foobar;" where nothing has ever been inserted at position 123 into map foobar,...
2
by: Kiel | last post by:
===== My error is: error C2512: no appropriate default constructor available I'm trying to use the default args for class B when it is an aggregate of class A. I could solve this problem with...
5
by: Mahesh Devjibhai Dhola | last post by:
Hi All, I want to make a custom class in c#, which extends System.Xml.XmlNode class of BCL. Now in custom class, I have implement abstract methods of XmlNode class also. Now when I am trying to...
5
by: sparks | last post by:
I am about to pull my hair out on this one. its a class stack with push and pop but one time its trying to find a } at the end of the code the next it says that I need a } after private man I...
4
by: Fedor Semenov | last post by:
How can I call a base class' constructor from a derived class. Suppose I have: class Button // Base class { public: Button(void) { // Register classes, create the button, etc. } };
10
by: Joel | last post by:
Is it true that if we don't specify a default constructor for our class, then the C# compiler provides us with its own that zeroes (or assigns default values) to the data members? I wrote a...
5
by: kkirtac | last post by:
Hi, i have a class "myClass", and i want to return two instances of the same class from a function. I define an array as follows : "myClass sample ;" and i initialize two instances: "myClass...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.