473,671 Members | 2,311 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(doub le x,double y, double larg, double haut)
: m_hautGauche(x, y), m_basDroite(x+l arg, y+haut){cout<<" constr rec
1"<<endl;}

Trectangle(Trec tangle& 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 3784
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(doub le x,double y, double larg, double haut)
: m_hautGauche(x, y), m_basDroite(x+l arg, y+haut){cout<<" constr rec
1"<<endl;}

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


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

| Trectangle(Trec tangle 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.co m> <http://www.dietmar-kuehl.de/>
<http://www.contendix.c om> - Software Development & Consulting

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

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

Trectangle(cons t 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(doub le x,double y, double larg, double haut)
: m_hautGauche(x, y), m_basDroite(x+l arg, y+haut){cout<<" constr rec
1"<<endl;}

Trectangle(Trec tangle&
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
"Dietmar Kuehl" <di***********@ yahoo.com> a écrit dans le message de news:
11************* *********@z14g2 00...legr oups.com...
stephane wrote:
Trectangle(doub le x,double y, double larg, double haut)
: m_hautGauche(x, y), m_basDroite(x+l arg, y+haut){cout<<" constr rec
1"<<endl;}

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


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

| Trectangle(Trec tangle 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.co m> <http://www.dietmar-kuehl.de/>
<http://www.contendix.c om> - Software Development & Consulting

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

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

Trectangle(cons t 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;
}
[...]

Jul 22 '05 #5

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

Similar topics

50
6343
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 def foo(self, data): self.attr1=data self.attr2.append(data) The initialization of attr1 is obviously OK, all instances of Father redefine it in the method foo. But the initialization of attr2 is wrong
7
28983
by: Dominique | last post by:
Suppose I have: class A { A(int x, int y); }; and class B: public A {
11
2545
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, the C++ standard states that a new element is created at position 123 and is initialized to zero. That is, in this case, the pair (123, 0) is inserted into the map. I am interested in knowing what the standard says should happen in the
2
1942
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 a point to B and initialize it in the C'tor of A, but logically it makes sense as an aggregate. =====
5
2138
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 run the class it gives an error that "System.Xml.XmlNode.XmlNode() is inaccessible due to its protection level". This error comes because XmlNode has not any public constructor. I found XmlNode has two constructor but both are private or friend...
5
1735
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 know I am kinda new but this is driving me up the wall .... Should I start over or what. I keep adding things trying to get it to work but no success.
4
3950
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
4693
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 no-parameter constructor for my class with an empty function body. I then instantiated an object and tried printing its values, amazingly the members were already initialized. But how is this possible if I have not included any code for doing so. The...
5
1592
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 sample1, sample2 ;" "sample = sample1 ; sample = sample2 ; return sample ;" This caused the error : error C2512 : 'myClass' : no appropriate default constructor available any idea how to return 2 instances and what is wrong with this...
0
8473
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8911
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8819
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8667
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7428
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6222
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4222
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2808
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.