简体   繁体   English

错误:没有匹配的函数可以调用'b :: b()'

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

Hi I'm just begin to program in C++ but I have a problem. 嗨,我只是开始用C ++编程,但是我有一个问题。 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 : 这是我的第一堂课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 : 这是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 : 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 : 和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. b的唯一构造函数采用两个double参数。

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. a是从b派生的,但是类a无法使用其两个必需参数来调用其超类的构造函数,并且其超类b没有默认的构造函数。

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. 当您定义没有构造函数的类时,C ++会为您生成一个默认构造函数。 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. 那么实际上就好像我们有一个默认的构造函数b() ,它什么也不做。 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) . 该类只有一个构造函数: 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: 即使我们从此类继承来创建一个具有默认构造函数的派生类a ,也是如此:

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. 因为我们没有在a::a()构造函数中显式初始化基b ,所以C ++编译器希望使用默认构造来创建对象的b部分。 But, alas, b has no default constructor! 但是,可惜, b没有默认构造函数!

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的部分a ,是这样的:

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; 此C ++特殊的base / member初始化语法,可在构造函数中使用以指定如何初始化成员和基础对象。 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. 第二种解决方案是在b添加默认构造函数,或者修改其现有的构造函数,使其可以充当默认构造函数。 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. 由于现在可以将b(int, double)构造函数称为不带参数的b() ,因此可以满足默认构造函数的需要。

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. 只要有可能,就应该以这种方式初始化成员,而不是让它们进行默认初始化,并用构造函数主体中的赋值覆盖成员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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