简体   繁体   中英

error: no matching function for call to 'b::b()'

Hi I'm just begin to program in C++ but I have a problem. I try to create a class in an other class but it does not work... Could you help me please?

this is my main :

#include <iostream>
#include "a.hpp"
#include "b.hpp"

using namespace std;



int main()
{

return 0;
}

and this is my first class a cpp :

#include <iostream>
#include "a.hpp"

using namespace std;

a::a(){} /* The problem is here :error: no matching function for call  
to 'b::b()*/
a::~a(){}

void a::write()
{

cout << "Write a number : " << endl;
cin >> m_number;
}

this is the hpp :

#ifndef A_HPP_INCLUDED
#define A_HPP_INCLUDED

#include "b.hpp"


class a : public b
{
public:
a();
~a();
void write();

protected:
int m_number;
};


#endif // INTERFACE_HPP_INCLUDED

the class b cpp :

#include <iostream>
#include "b.hpp"


using namespace std;
b::b(double c, double d){m_c = c; m_d = d}
b::~b(){}

void b::write1()
{
cout << m_c+m_d<< endl;
}

and the hpp :

#ifndef B_HPP_INCLUDED
#define B_HPP_INCLUDED


class b
{
public:
b(double c, double d);
~b();


void writed1();

protected:
double m_c;
double m_d;
};


#endif // B_HPP_INCLUDED

Thanks a lot for your help !!!

Class b 's only constructor takes two double parameters.

Class a is derived from b , but class a fails to invoke its superclass's constructor with its two required parameters, and its superclass, class b , does not have a default constructor.

If you class's constructor has required parameters, and the class does not have a default constructor, and you derive from the class, the derived class is responsible for constructing its superclass, and invoking its constructor with the required parameters.

When you define a class without a constructor, C++ generates a default constructor for you. For instance, if we write a "plain old structure":

struct b {
  int x;
};

then effectively it's as if we have a default constructor b() which does nothing. And that allows us to write:

b bvar;

However, when we introduce a constructor, the compiler-generated default constructor is suppressed:

class b {
public:
   b(int i, double d);
};

this class has one and only one constructor: b(int, double) . There is no way to construct an object without calling this constructor and passing it two parameters:

b bvar;  // error: no b::b() function.

This remains true even if we inherit from this class to create a derived class a which does have a default constructor:

class a : public b {
public:
  a();
};

a::a()
{

}

Because we didn't explicitly initialize the base b in the a::a() constructor, the C++ compiler wants to use the default construction to create the b part of the object. But, alas, b has no default constructor!

There are two ways out. One is to add explicit syntax into the a constructor to initialize the b part of a , like this:

a() : b(0, 3.5)
{
}

This C++'s special the base/member initialization syntax that you can use in constructors to specify how member and base objects are initialized; by means of this you can pass them the constructor arguments they require.

The second solution is either to add a default constructor to b , or else modify its existing constructor so that it can behave as a default constructor. Let's take the second approach:

class b {
public:
   b(int i = 0, double d = 0.0);
};

What we did is give default values to both arguments, making them optional. Because the b(int, double) constructor now can be called as b() with no arguments, it satisfies the need for a default constructor.

By the way, multiple initializers can be specified for bases and members. The initialization takes place in the order in which these are defined, not in the order in which the initializers are written:

class foo : public bar {
   xyzzy xy;
   int count;
public:
   foo(const string &arg)
   : bar(arg)  // let's assume the base constructor takes a string
   , xy(42, 9) // class xyzzy has a constructor that takes a pair of integers
   , count(0)  // member, initialized to zero.
   {
   } 
}

This also illustrates not a bad way of laying out these initializers in the program text, with the colon and commas on the left as if they were prefixes.

Whenever possible, members should be initialized in this way, rather than being left to default initialization and the overwritten with assignments in the constructor body.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM