在另一个构造函数中调用构造函数(没有要调用的匹配函数..) c++
Call constructor inside another constructor (no matching function to call...) c++
我编写了一个数组类来创建 1d、2d 和 3d 数组,它适用于每个测试: 2d 情况数组类的构造函数示例:
Array::Array( int xSize, int ySize )
{
xSize_ = xSize;
ySize_ = ySize;
zSize_ = 1;
vec.resize(xSize*ySize);
}
它工作正常,但是当我需要在其他构造函数中使用此构造函数时,我得到"不匹配函数错误",我的部分代码:
class StaggeredGrid
{
public:
StaggeredGrid ( int xSize1, int ySize1, real dx, real dy ) : p_ (2,2) {}
protected:
Array p_;
完全错误:
No matching function for call to Array::Array()
Candidates are : Array::Array(int)
Array::Array(int, int)
Array::Array(int, int, int)
如果有人知道这个问题,我将不胜感激
数组类有三个构造函数,分别采用一个、两个和三个整数。如果 StaggeringGrid 有一个默认的构造函数,它将调用 Array::Array(),它不存在,从你告诉的内容。
问题是,然后你声明并且不要在StaggeredGrid的构造函数中初始化
Array p_;
应该调用默认构造函数,代码中似乎缺少该构造函数。
简单地添加空的默认构造函数应该可以解决问题。
class Array
{
public:
Array(){}
// ...
};
在类中定义任何构造函数后,编译器不会为类定义隐式默认构造函数。
在您的情况下,您已经定义了参数化构造函数"Array(int xSize, int ySize )",但您正在使用默认构造函数创建一个类,即数组p_。这将调用编译器未完全找到的默认构造函数。
溶液:
在数组类中引入默认构造函数
相关文章:
- 在c++中使用向量时,如何调用构造函数和析构函数
- C++:考虑但不调用构造函数的特殊性
- 对象实例化调用构造函数的次数太多
- 我使用向量来创建类对象列表.初始化向量时如何使用参数调用构造函数?
- C ++:通过大括号调用构造函数?
- 不能调用构造函数
- 赋值 boost::intrusive_ptr 而不调用构造函数?
- 在模板化类的构造函数中调用构造函数
- 如何为 std::vector 分配内存,然后稍后为某些元素调用构造函数?
- 为什么从另一个构造函数内部调用C++构造函数不修改类变量?
- 静态 std::map instatiation 在类的方法中调用构造函数吗?
- 有没有一种简单的方法可以在对象向量上调用构造函数?
- 我不明白在这个例子中什么时候调用构造函数
- 调用c++构造函数的不同方法
- 调用构造函数与将内联常量定义为默认参数
- 如何通过 Rust FFI 调用C++构造函数?
- "new"运算符是否总是调用构造函数?
- 无法调用构造函数
- 使用 "()" 调用构造函数不同于"{}"
- 确定是调用构造函数还是强制转换运算符的因素