Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 矩阵转置模板_C++_Templates_Matrix - Fatal编程技术网

C++ 矩阵转置模板

C++ 矩阵转置模板,c++,templates,matrix,C++,Templates,Matrix,当我尝试使用模板为矩阵转置编写代码时,我有以下错误 错误1错误C2248:“矩阵::元素”:无法访问类“矩阵”中声明的私有成员 有人能告诉我如何修复这个错误吗 //Matrix.h #include <iostream> #include <vector> using namespace std; template<class T, int m, int n> class Matrix; template<class T, int m, int n

当我尝试使用模板为矩阵转置编写代码时,我有以下错误

错误1错误C2248:“矩阵::元素”:无法访问类“矩阵”中声明的私有成员

有人能告诉我如何修复这个错误吗

//Matrix.h
#include <iostream>
#include <vector>
using namespace std;

template<class T, int m, int n>
class Matrix;


template<class T, int m, int n>
class Matrix
{
vector<vector<T>> elements;
int nrow;
int ncol;

public:
Matrix();
~Matrix();
void print();

Matrix<T, n, m> transpose();

};

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, i);
    elements.push_back(row);
}
}

template<class T, int m, int n>
Matrix<T, m, n>::~Matrix(){}

template<class T, int m, int n>
Matrix<T, n, m> Matrix<T, m, n>::transpose(){
Matrix<T, n, m> m;
for (int i = 0; i < nrow; ++i){
    for (int j = 0; j < ncol; ++j){
        m.elements[j][i] = elements[i][j];
    }
}
return m;
}

template<class T, int m, int n>
void Matrix<T, m, n>::print()
{
for (int i = 0; i < nrow; i++){
    for (int j = 0; j < ncol; j++)
    {
        cout << elements[i][j] << " ";
    }
    cout << endl;
}
}

//main.cpp

#include "Matrix.h"
using namespace std;

int main()
{
Matrix<int, 3, 2> a;
Matrix<int, 3, 2> b;
Matrix<int, 2, 3> c;
c = a.transpose();
c.print();
}
//Matrix.h
#包括
#包括
使用名称空间std;
模板
类矩阵;
模板
类矩阵
{
矢量元素;
int nrow;
int ncol;
公众:
矩阵();
~Matrix();
作废打印();
矩阵转置();
};
模板
矩阵::矩阵():nrow(m),ncol(n)
{
对于(int i=0;icout问题是当
m
n
不相同时,
Matrix
Matrix
是不同的类。一个不能访问另一个的
private
成员。您可以通过提供
public
成员函数来避免访问问题余烬变量

如果
m
n
相同,您就不会注意到问题。

Matrix
Matrix
m!=n
时是不同的类。这意味着您无法在
Matrix
的成员函数中访问
Matrix
的私有成员

您可以添加好友声明来解决此问题:

template<class T, int m, int n>
class Matrix
{

    vector<vector<T>> elements;
    int nrow;
    int ncol;

    public:
    Matrix();
    ~Matrix();
    void print();

    Matrix<T, n, m> transpose();

    friend class Matrix<T, n, m>;
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
};
模板
类矩阵
{
矢量元素;
int nrow;
int ncol;
公众:
矩阵();
~Matrix();
作废打印();
矩阵转置();
友元类矩阵;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
};

错误与代码中的以下函数有关:

template<class T, int m, int n>
Matrix<T, n, m> Matrix<T, m, n>::transpose() {
    Matrix<T, n, m> m;
    for (int i = 0; i < nrow; ++i) {
        for (int j = 0; j < ncol; ++j) {
            m.elements[j][i] = elements[i][j];
        }
    }
    return m;
}
然后,有问题的函数应该如下所示:

template<class T, int m, int n>
Matrix<T, n, m> Matrix<T, m, n>::transpose() {
    Matrix<T, n, m> m;
    for (int i = 0; i < nrow; ++i) {
        for (int j = 0; j < ncol; ++j) {
            m.get_elements_ptr()->at(j)[i] = elements[i][j];
        }
    }
    return m;
}
模板
矩阵::转置(){
矩阵m;
对于(int i=0;iat(j)[i]=elements[i][j];
}
}
返回m;
}
这将解决您的问题。不过,顺便说一句,我认为您的项目设计可以进行一些调整,以完全避免在另一个矩阵对象中包含矩阵。您是否考虑过使用指向动态分配的矩阵的指针?这将大大减少您的程序复制量,并将改善这两个问题大大提高了程序的内存和运行时效率


希望这有帮助!

您对模板参数和变量都使用了
m
。使它们不同。
template<class T, int m, int n>
Matrix<T, n, m> Matrix<T, m, n>::transpose() {
    Matrix<T, n, m> m;
    for (int i = 0; i < nrow; ++i) {
        for (int j = 0; j < ncol; ++j) {
            m.get_elements_ptr()->at(j)[i] = elements[i][j];
        }
    }
    return m;
}