创建一个对象数组,没有匹配的构造函数初始化错误
Create an array of objects, Error of no matching constructor initialization
我正在尝试创建对象BuyOrder的数组
BuyOrder buy[10];
为什么我得到错误说"没有匹配的构造函数初始化的BuyOrder[10]"?
下面是BuyOrder构造函数。我必须创建另一个默认构造函数吗?
BuyOrder::BuyOrder(double price, int quantity, Stock &s)
:buyPrice{ price },
buyQuantity{quantity},
buyStock{ s }
{}
就像我在评论中说的,你可以为小数组做聚合初始化。
#include <array>
struct example
{
example(int, double) {}
example(example const&) = delete;
};
int main() {
example arr1[2] {
{1, 3.4},
{2, 5.6}
};
std::array<example, 2> arr2 {{
{1, 3.4},
{2, 5.6}
}};
return 0;
}
相关文章:
- 有没有一种代码密度较低的方法来使用非默认构造函数初始化数组?
- 如何在 C++ 中使用它的构造函数初始化 unique_ptrs 的 2D 向量?
- 在C++中使用默认构造函数初始化对象的不同方法
- 使用默认构造函数初始化对象的不同方法
- 我们可以用参数化构造函数初始化结构的数组吗?
- 有没有办法使用该类的构造函数初始化另一个类的私有部分内的对象数组?
- 类内初始化与构造函数初始化列表的顺序
- 通过 C++ 中的重载构造函数初始化未知类型的变量
- 使用复制构造函数初始化 new[]
- 如何从子类的构造函数初始化父类的私有成员
- 在构造函数初始化列表中使用 std::variant
- 使用构造函数初始化结构还是在之后设置其值更好?
- C++ 没有匹配的构造函数初始化 []
- 我正在使用dev c ++,但收到错误(C++98'array'必须由构造函数初始化)
- 使用所述填充构造函数初始化向量中的向量
- 如何同时创建一个具有两个或多个构造函数初始化的对象
- 在 new 关键字中,由默认构造函数初始化的类中的元素是否也使用 new 关键字在C++?
- 这是使用构造函数初始化数组对象的最佳方法吗?
- 内置类型的构造函数初始化
- 构造函数初始化和对象损坏