C ++错误:没有匹配的构造函数用于初始化 [英] C++ error: no matching constructor for initialization of

查看:694
本文介绍了C ++错误:没有匹配的构造函数用于初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用C ++进行成员分配,您可以在其中将一个对象的值设置为同一类的另一个对象.该程序的想法是使用一些值初始化一个矩形对象,然后创建另一个矩形对象,但是将第一个的值分配给第二个.

Im practicing memberwise assignment in C++, where you can set the values of one object to another object of the same class. The idea of the program is to initialize a rectangle object with some values and create another rectangle object but assign the value of the first into the second.

它给了我一个错误,该错误在下面发布,我无法弄清楚它是什么,并且让我发疯了大声笑

Its giving me an error, which is posted below, and I can't figure out what it is and its driving me nuts lol

这是我的Rectangle.h

This is my 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

This is my 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.

这就是我能够使其工作的方式.我将所有内容都移到了rectangular.cpp中,并为构造函数提供了默认参数.

This is how I was able to make it work. I moved everything into rectangle.cpp and gave the constructor default arguments.

已编辑矩形.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中时,它将无法工作.但是,当我将类和成员函数的定义移到rectangle.cpp时,它就可以工作了.因此,我让程序正常运行,但我没有解决真正的问题,即当类和成员函数定义位于矩形中时.h,它将无法编译.

The only changes I made were giving default arguments to my user-defined constructor. However, it wasn't able to work when the changes were in rectangle.h. However, when I moved the class and member function definitions to rectangle.cpp it was able to work. So, I got the program to work but I didn't address the real issue, which is when the class and member function definitions are in rectangle.h, it won't compile.

如果有人遇到此问题并找到解决方案,请告诉我您的处理方式.谢谢:)

If anyone has faced this problem and has found a solution to this, please let me know how you did it. Thanks :)

推荐答案

在线

Rectangle box2; // no default constructor, error

您正在尝试调用Rectangle的默认构造函数.编译器不再生成这样的默认构造函数,因为您的Rectangle具有用户定义的构造函数,该构造函数带有2个参数.因此,您需要指定参数,例如

you are trying to invoke the default constructor of Rectangle. The compiler does not generate such a default constructor anymore, because your Rectangle has a user defined constructor that takes 2 parameters. Therefore, you need to specify the parameters, like

Rectangle box2(0,10);

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

The error I get when compiling your code is:

Rectangle.cpp:8:15:错误:没有匹配的函数来调用'Rectangle :: Rectangle()' 矩形框2;

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

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

A solution is to create a default constructor for Rectangle, since it is not automatically generated anymore due to your user defined one:

Rectangle(); // in Rectangle.h

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

另一种解决方案(也是首选的解决方案,因为它不会使数据保持未初始化状态)是为现有构造函数分配默认参数.

Another solution (and the preferable one, since it doesn't leave the data un-initialized) is to assign default arguments to your existing constructor.

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

通过这种方式,您可以将类设置为Default-Constructible.

In this way, you make your class Default-Constructible.

这篇关于C ++错误:没有匹配的构造函数用于初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆