C++ 错误:没有用于初始化的匹配构造函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30271811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ error: no matching constructor for initialization of
提问by skipper
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.
我在 C++ 中练习成员赋值,您可以在其中将一个对象的值设置为同一类的另一个对象。该程序的想法是用一些值初始化一个矩形对象并创建另一个矩形对象,但将第一个的值分配给第二个。
Its giving me an error, which is posted below, and I can't figure out what it is and its driving me nuts lol
它给了我一个错误,它发布在下面,我无法弄清楚它是什么,它让我发疯了哈哈
This is my Rectangle.h
这是我的 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
This is my Rectangle.cpp
这是我的 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;
}
This is the error when I compile.
这是我编译时的错误。
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.
EDIT: This is how I was able to make it work. I moved everything into rectangle.cpp and gave the constructor default arguments.
编辑:这就是我能够让它工作的方式。我将所有内容都移到了 rectangle.cpp 中,并为构造函数提供了默认参数。
EDITED rectangle.cpp
编辑矩形.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; }
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.
我所做的唯一更改是为用户定义的构造函数提供默认参数。但是,当更改在 rectangle.h 中时,它无法工作。但是,当我将类和成员函数定义移动到 rectangle.cpp 时,它就可以工作了。所以,我让程序运行了,但我没有解决真正的问题,即当类和成员函数定义在 rectangle.h 中时,它不会编译。
If anyone has faced this problem and has found a solution to this, please let me know how you did it. Thanks :)
如果有人遇到过这个问题并找到了解决方案,请告诉我你是如何做到的。谢谢 :)
采纳答案by vsoftco
In the line
在行中
Rectangle box2; // no default constructor, error
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
. 编译器不再生成这样的默认构造函数,因为您Rectangle
有一个带有 2 个参数的用户定义的构造函数。因此,您需要指定参数,例如
Rectangle box2(0,10);
The error I get when compiling your code is:
编译代码时出现的错误是:
Rectangle.cpp:8:15: error: no matching function for call to 'Rectangle::Rectangle()' Rectangle box2;
Rectangle.cpp:8:15: 错误:没有匹配的函数来调用 'Rectangle::Rectangle()' Rectangle box2;
A solution is to create a default constructor for Rectangle
, since it is not automatically generated anymore due to your user defined one:
一种解决方案是为 创build一个默认构造函数Rectangle
,因为由于您的用户定义,它不再自动生成:
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
In this way, you make your class Default-Constructible.
通过这种方式,您可以使您的类默认可构造。
回答by Steve
A compiler generated default constructor is only generated if you have no defined constructors. You define a constructor, so if you want a default constructor you have to provide it yourself. Probably the easiest (arguably) is to provide it by using default arguments in your two argument constructor:
仅当您没有定义的构造函数时,才会生成编译器生成的默认构造函数。你定义了一个构造函数,所以如果你想要一个默认的构造函数,你必须自己提供它。可能最简单的(可以说)是通过在你的两个参数构造函数中使用默认参数来提供它:
Rectangle(double l=0, double w=0)
Also you should use the inline
keyword as shown below or you may find you get linker errors:
此外,您应该使用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; }