C ++ Matrix类模板继承

时间:2016-12-19 21:32:40

标签: c++ class templates inheritance matrix

我最近开始用c ++编写Matrix类。矩阵可以是正方形或不是正方形。所以我首先创建了一个Matrix基类模板。然后,我想实现一个SquareMatrix类,以处理反转和其他类似的东西。但它显然没有用,我得到了以下错误:

我的代码(在代码块上使用GCC):

Matrix.hpp:

template<unsigned int M, unsigned int N>
class Matrix
{
public:
    Matrix() {}

    ...

private:
    std::vector<float> mElements;
};

SquareMatrix.hpp:

#include "Matrix.hpp"

template<unsigned int M>
class SquareMatrix: public Matrix<M, M>
{
    ...
};

构建日志:

||=== Build: Debug in KirchhofSolver (compiler: GNU GCC Compiler) ===|
F:\Data\Programmation\KirchhoffSolver\SquareMatrix.hpp|7|error: expected template-name before '<' token|
F:\Data\Programmation\KirchhoffSolver\SquareMatrix.hpp|7|error: expected '{' before '<' token|
F:\Data\Programmation\KirchhoffSolver\SquareMatrix.hpp|7|error: expected unqualified-id before '<' token|
||=== Build failed: 3 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

如果你能帮助我,我会很感激,因为我已经花了很长时间在网上浏览我的问题的答案,我找不到任何东西!

2 个答案:

答案 0 :(得分:0)

这是我在一段时间后工作的Matrix类模板,它允许MxN以及MxNxO矩阵。我只有2个方法而不是构造函数,那就是将矩阵设置为零并生成单位矩阵。也许这对你有用,但是我没有试图从这个班级“继承”。

#include <iostream>

// template<typename TYPE, X, Y, Z>
template<typename T, unsigned X, unsigned Y, unsigned Z = 1>
class Matrix {
public:
    const unsigned x_size = X;
    const unsigned y_size = Y; 
    const unsigned z_size = Z;
    T matrix_[X][Y][Z];

private:

    T identity_[X][Y][Z];

public:
    Matrix();
    Matrix( T elements[X][Y][Z] );

    void identity( T identity[X][Y][Z] );
    void matrix( T matrix[X][Y][Z] );

private:
    void zeroMatrix();
    void makeIdentity();

};

template<typename T, unsigned X, unsigned Y, unsigned Z>
Matrix<T, X, Y, Z>::Matrix() {
    zeroMatrix();
    makeIdentity();
}

template<typename T, unsigned X, unsigned Y, unsigned Z>
Matrix<T, X, Y, Z>::Matrix( T elements[X][Y][Z] ) {
    for ( unsigned k = 0; k < z_size; k++ ) {
        for ( unsigned j = 0; j < y_size; j++ ) {
            for ( unsigned i = 0; i < z_size; i++ ) {
                matrix_[i][j][k] = elements[i][j][k];
            }
        }
    }
}

template<typename T, unsigned X, unsigned Y, unsigned Z>
void Matrix<T, X, Y, Z>::zeroMatrix() {
    for ( unsigned k = 0; k < z_size; k++ ) {
        for ( unsigned j = 0; j < y_size; j++ ) {
            for ( unsigned i = 0; i < x_size; i++ ) {
                matrix_[i][j][k] = 0;
            }
        }
    }
}

template<typename T, unsigned X, unsigned Y, unsigned Z>
void Matrix<T, X, Y, Z>::matrix( T matrix[X][Y][Z] ) {
    for ( unsigned k = 0; k < z_size; k++ ) {
        for ( unsigned j = 0; j < y_size; j++ ) {
            for ( unsigned i = 0; i < x_size; i++ ) {
                matrix[i][j][k] = matrix_[i][j][k];
            }
        }
    }
}

template<typename T, unsigned X, unsigned Y, unsigned Z>
void Matrix<T, X, Y, Z>::makeIdentity() {

    if ( z_size > 1 ) {
        for ( unsigned k = 0; k < z_size; k++ ) {
            for ( unsigned j = 0; j < y_size; j++ ) {
                for ( unsigned i = 0; i < x_size; i++ ) {
                    if ( i == j && i == k && j == k ) {
                        identity_[i][j][k] = 1;
                    } else {
                        identity_[i][j][k] = 0;
                    }
                }
            }
        }
    } else {
        for ( unsigned j = 0; j < y_size; j++ ) {
            for ( unsigned i = 0; i < x_size; i++ ) {
                if ( i == j ) {
                    identity_[i][j][0] = 1; 
                } else {
                    identity_[i][j][0] = 0;         
                }
            }
        }
    }
}

template<typename T, unsigned X, unsigned Y, unsigned Z>
void Matrix<T, X, Y, Z>::identity( T identity[X][Y][Z] ) {
    for ( unsigned k = 0; k < z_size; k++ ) {
        for ( unsigned j = 0; j < y_size; j++ ) {
            for ( unsigned i = 0; i < x_size; i++ ) {
                identity[i][j][k] = identity_[i][j][k];
            }
        }
    }
}

int main() {

    const int x = 4;
    const int y = 4;
    const int z = 4;

    // MxNxO      
    Matrix<int,x,y,z> a;

    int identity3D[x][y][z] = {0};
    a.identity( identity3D );

    std::cout << "MxNxO Matrix Identity of Integer Type: " << std::endl;
    for ( unsigned k = 0; k < z; k++ ) {
        for ( unsigned j = 0; j < y; j++ ) {        
            for ( unsigned i = 0; i < x; i++ ) {
            std::cout << identity3D[i][j][k] << " ";
            }
            std::cout << std::endl;
        }
        std::cout << std::endl;
    }

    std::cout << std::endl << std::endl;

    // MxN Matrix Identity of Float Type - using last template argument with a value of 1.
    const int a = 3;
    const int b = 4;
    const int c = 1;
    Matrix<float,a,b,c> b;

    float idenity2D[a][b][c] = {0};  // Still Need To Use 3RD Array But Set to 1.
    std::cout << "MxN Matrix Identity of Float Type: " << std::endl;

    for ( unsigned k = 0; k < c; k++ ) {
        for ( unsigned j = 0; j < b; j++ ) {        
            for ( unsigned i = 0; i < a; i++ ) {
                std::cout << identity2D[i][j][k] << " ";
            }
            std::cout << std::endl;
        }
        std::cout << std::endl;
    }

    return 0; 
}

我还没有完全完成这个类,我对这种类型的矩阵类的原始想法更像是一个存储类而不是一个可操作的数学类或结构。我正在设计这个来存储不同类型的对象,从不同的数字原子类型到类,结构甚至函数指针。我只添加了“零方法”和“身份”功能,以确保我正确地构建它。但是,这在Windows 7 - x64上的MSVS2015CE中编译和运行时没有错误。随意使用它,在它上面构建,改进它甚至完全拒绝它。

答案 1 :(得分:0)

我会自杀,因为这只是一个标题声明问题,与我向您展示的代码无关。所以我的问题解决了!