C++ error: no matching constructor for initialization of

C++ error: no matching constructor for initialization of

我正在练习 C++ 中的成员赋值,您可以在其中将一个对象的值设置为相同 class 的另一个对象。该程序的想法是用一些值初始化一个矩形对象并创建另一个矩形对象,但将第一个的值赋给第二个。

它给我一个错误,已发布在下面,我无法弄清楚它是什么,它让我发疯哈哈

这是我的Rectangle.h

#ifndef RECTANGLE_H
#define RECTANGLE_H

class Rectangle {
    private:
        double length;
        double width;
    public:
        Rectangle(double, double);
        double getLength() const;
        double getWidth() const;
};

Rectangle::Rectangle(double l, double w) {
    length = l;
    width = w;
}

double Rectangle::getWidth() const { return width; }
double Rectangle::getLength() const { return length; }

#endif

这是我的Rectangle.cpp

#include <iostream>
#include "rectangle.h"
using namespace std;

int main()
{
    Rectangle box1(10.0, 10.0);
    Rectangle box2;

    cout << "box1's width and length: " << box1.getWidth() << ", " << box1.getLength() << endl;
    cout << "box2's width and length: " << box2.getWidth() << ", " << box2.getLength() << endl;

    box2 = box1;

    cout << "box1's width and length: " << box1.getWidth() << ", " << box1.getLength() << endl;
    cout << "box2's width and length: " << box2.getWidth() << ", " << box2.getLength() << endl;

    return 0;
}

这是我编译时的错误

skipper~/Desktop/Programming/Memberwise: g++ rectangle.cpp 
rectangle.cpp:7:12: error: no matching constructor for initialization of
      'Rectangle'
        Rectangle box1(10.0, 10.0);
                  ^    ~~~~~~~~~~
./rectangle.h:4:7: note: candidate constructor (the implicit copy constructor)
      not viable: requires 1 argument, but 2 were provided
class Rectangle {
  ^
./rectangle.h:4:7: note: candidate constructor
      (the implicit default constructor) not viable: requires 0 arguments, but 2
      were provided
1 error generated.

编辑:这就是我让它工作的方式。我将所有内容移至 rectangle.cpp 并为构造函数提供了默认参数。

编辑rectangle.cpp

#include <iostream>
using namespace std;

class Rectangle {
     private:
         double length;
         double width;
    public:
        //Rectangle();
        Rectangle(double = 0.0, double = 0.0);
        double getLength() const;
        double getWidth() const;
 };

 int main()
 {
    Rectangle box1(10.0, 10.0);
    Rectangle box2;

    cout << "box1's width and length: " << box1.getWidth() << ", " << box1.getLength() << endl;
    cout << "box2's width and length: " << box2.getWidth() << ", " << box2.getLength() << endl;

    box2 = box1;

    cout << "box1's width and length: " << box1.getWidth() << ", " << box1.getLength() << endl;
    cout << "box2's width and length: " << box2.getWidth() << ", " << box2.getLength() << endl;

    return 0;
}

Rectangle::Rectangle(double l, double w) {
    length = l;
    width = w;
}

double Rectangle::getWidth() const { return width; }
double Rectangle::getLength() const { return length; }

我所做的唯一更改是为用户定义的构造函数提供默认参数。但是,当更改在 rectangle.h 中时,它无法工作。但是,当我将 class 和成员函数定义移动到 rectangle.cpp 时,它就可以工作了。所以,我让程序运行,但我没有解决真正的问题,即当 class 和成员函数定义在 rectangle.h 中时,它不会编译。

如果有人遇到过这个问题并找到了解决办法,请告诉我你是怎么做到的。谢谢:)

在行

Rectangle box2; // no default constructor, error

您正在尝试调用 Rectangle 的默认构造函数。编译器不再生成这样的默认构造函数,因为您的 Rectangle 有一个带 2 个参数的用户定义构造函数。所以需要指定参数,比如

Rectangle box2(0,10);

我在编译你的代码时得到的错误是:

Rectangle.cpp:8:15: error: no matching function for call to 'Rectangle::Rectangle()' Rectangle box2;

一个解决方案是为 Rectangle 创建一个默认构造函数,因为它不再自动生成,因为你的用户定义了一个:

Rectangle(); // in Rectangle.h

Rectangle::Rectangle(){} // in Rectangle.cpp (or Rectangle::Rectangle() = default; in C++11)

另一种解决方案(也是更好的解决方案,因为它不会使数据处于未初始化状态)是将默认参数分配给现有的构造函数。

Rectangle::Rectangle(double l = 0, double w = 0); // only in Rectangle.h

通过这种方式,您可以使 class 默认构造。

编译器生成的默认构造函数只有在您没有定义的构造函数时才会生成。你定义了一个构造函数,所以如果你想要一个默认的构造函数,你必须自己提供它。可能最简单的(可以说)是通过在你的双参数构造函数中使用默认参数来提供它:

Rectangle(double l=0, double w=0)

您还应该使用 inline 关键字,如下所示,否则您可能会发现链接器错误:

inline Rectangle::Rectangle(double l, double w) {
    length = l;
    width = w;
}

inline double Rectangle::getWidth() const { return width; }
inline double Rectangle::getLength() const { return length; }