gpt4 book ai didi

c++ - C++错误: no matching constructor for initialization of

转载 作者:行者123 更新时间:2023-12-02 10:58:18 25 4
gpt4 key购买 nike

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

它给我一个错误,在下面发布,我不知道这是什么,它使我发狂。

这是我的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.

编辑:这就是我能够使其工作的方式。我将所有内容都移至rectangular.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; }

我所做的唯一更改是为用户定义的构造函数提供了默认参数。但是,当更改在angle.h中时,它无法工作。但是,当我将类和成员函数的定义移至rectangle.cpp时,它就可以工作。所以,我让程序正常工作,但我没有解决真正的问题,即当类和成员函数定义在矩形中时。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

这样,您就可以将类设为Default-Constructible。

关于c++ - C++错误: no matching constructor for initialization of,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53416270/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com