请帮我纠正以下错误。
错误1错误C2248:'SquareMatrix :: SquareMatrix':不能 访问在“SquareMatrix”类中声明的私有成员
我正在尝试在类(矩阵)下实现子类(方形矩阵),我不知道如何修复错误。这是代码。
#ifndef _MATRIX_
#define _MATRIX_
#include <iostream>
#include <vector>
using namespace std;
//class matrix starts here
template<class T, int m, int n>
class Matrix{
vector<vector<T>> elements;
int nrow;
int ncol;
public:
Matrix();
~Matrix();
template<class T, int m, int n>
Matrix<T, m, n>::Matrix() : nrow(m), ncol(n){
for (int i = 0; i < nrow; i++){
vector<T> row(ncol, 0);
elements.push_back(row);
}
}
template<class T, int m, int n>
Matrix<T, m, n>::~Matrix(){}
//here is the inheritance class SquareMatrix
template<class T, int n>
class SquareMatrix : public Matrix<T, n, n>{
SquareMatrix();
};
template<class T, int n>
SquareMatrix<T, n>::SquareMatrix() :nrow(n), ncol(n){
for (int i = 0; i < nrow; i++){
vector<T> row(ncol, 0);
elements.push_back(row);
}
}
//Here is the main.cpp
#include "Matrix.h"
using namespace std;
int main(){
Matrix<double, 3, 2> a;
SquareMatrix<double, 3> c;
}
答案 0 :(得分:2)
通过尝试在派生类构造函数的成员初始化列表中初始化 base 类的成员变量来体验表面的问题。除非基础是虚拟基础,否则不需要,甚至不允许。相反,只需将can踢到基类的路上,并让其构造函数初始化这些成员:
template<class T, int n>
SquareMatrix<T, n>::SquareMatrix() : Matrix<T,n,n>()
{
}
如果您没有注意到,这也会修复您发布的代码中的另一个逻辑缺陷。填充实际的elements
向量已经由您的基类完成,因此您也不需要在派生类中使用它。调用基类构造会为您处理它。
<强>改进强>
以下列出了可以大大简化此代码的事项:
std::array<T,n>
作为行类型,并将其包装在动态std::vector
中。 1}}大小m
ncol
和nrow
成员在矩阵的生命周期内永远不会改变,因为它们实际上是基于模板参数的。如果你想保留它们,请将它们更改为公共静态constexpr
(但老实说,我认为没有理由; m
和n
出现在模板的任何位置。using
别名即可。最后,代码简化为:
#include <vector>
#include <array>
// matrix managed on the heap as a vector of arrays
template<class T, size_t m, size_t n>
class Matrix
{
std::vector<std::array<T, n>> elements;
public:
static constexpr size_t nrows = m;
static constexpr size_t ncols = n;
Matrix() : elements(m)
{
}
};
派生的方阵矩阵远远减少,它可以是一个简单的using
别名:
// a square matrix built via a regular matrix
template<class T, size_t m>
using SquareMatrix = Matrix<T,m,m>;
当然,仍然需要以某种方式访问elements
成员,否则大部分都是毫无意义的。
祝你好运,我希望它有所帮助。
答案 1 :(得分:1)
SquareMatrix<T, n>::SquareMatrix() :nrow(n), ncol(n){
此处的构造函数正在尝试初始化其成员nrow
和ncol
。
当然,问题在于此类没有任何名为nrow
和ncol
的成员。他们是超类的私人成员。
模板是一个类。类的构造函数只能初始化自己的成员。它无法初始化其超类的任何成员。超类的构造函数负责初始化自己的成员。
您没有指定您正在使用的编译器,但这是一个非常可怕的错误消息。 gcc
的错误消息更有意义:
t.C:40:37: error: class ‘SquareMatrix<T, n>’ does not have any field named ‘nrow’
SquareMatrix<T, n>::SquareMatrix() :nrow(n), ncol(n){
P.S。帮自己一个忙,完全forget about "using namespace std;"。只是假装在C ++中没有这样的东西。特别是在头文件中。