简单矩阵类,错误:调用私有构造函数
Simple Matrix class, error: calling private constructor
我正在学习C++并尝试构建一个简单的矩阵类。我的基本情况将矩阵类定义为:
class Matrix {
int r; // number of rows
int c; // number of columns
double* d; // array of doubles to hold matrix values
Matrix(int nrows, int ncols, double ini = 0.0);
~Matrix();
}
构造函数/析构函数是:
Matrix::Matrix(int nrows, int ncols, double ini) {
r = nrows;
c = ncols;
d = new double[nrows*ncols];
for (int i = 0; i < nrows*ncols; i++) d[i] = ini;
}
Matrix::~Matrix() {
delete[] d;
}
问题:当我通过调用Matrix my_matrix(2,3)
实例化类 Matrix 时,出现以下错误:error: calling a private constructor of class 'Matrix'
、error: variable of type 'Matrix' has private destructor
。
问:为什么会这样?我如何理解什么是失败?有人可以指出我的解决方案/阅读材料来帮助我理解这个问题。感谢您的帮助!
默认情况下,对类的属性/方法的访问是私有的。在类中添加public:
语句:
class Matrix {
int r; // number of rows
int c; // number of columns
double* d; // array of doubles to hold matrix values
public:
Matrix(int nrows, int ncols, double ini = 0.0);
~Matrix();
}
相关文章:
- 在c++中使用向量时,如何调用构造函数和析构函数
- C++:考虑但不调用构造函数的特殊性
- 对象实例化调用构造函数的次数太多
- 我使用向量来创建类对象列表.初始化向量时如何使用参数调用构造函数?
- C ++:通过大括号调用构造函数?
- 不能调用构造函数
- 赋值 boost::intrusive_ptr 而不调用构造函数?
- 在模板化类的构造函数中调用构造函数
- 如何为 std::vector 分配内存,然后稍后为某些元素调用构造函数?
- 为什么从另一个构造函数内部调用C++构造函数不修改类变量?
- 静态 std::map instatiation 在类的方法中调用构造函数吗?
- 有没有一种简单的方法可以在对象向量上调用构造函数?
- 我不明白在这个例子中什么时候调用构造函数
- 调用c++构造函数的不同方法
- 调用构造函数与将内联常量定义为默认参数
- 如何通过 Rust FFI 调用C++构造函数?
- "new"运算符是否总是调用构造函数?
- 无法调用构造函数
- 使用 "()" 调用构造函数不同于"{}"
- 确定是调用构造函数还是强制转换运算符的因素