简体   繁体   English

实现模板Matrix类

[英]Implementing a template Matrix class

Suppose that I have the following code: 假设我有以下代码:

int main(){

    class Initializer {
    public:
        double operator()(int i, int j) const {
            return i + j;
        }
    };
    Matrix<2,5> m1;
    Matrix<2,5> m2(7);
    Matrix<1,3> m3(Initializer());

    m1(2,3) = 6;
    m2 += m1;
    Matrix<2,5> m4 = m1 + m2;
    return 0;
}

And I should implement a generic Matrix to make the above code to compile and work. 而且我应该实现一个通用的Matrix,以使上述代码得以编译和工作。 With my current implementation I have the following compilation errors and I'm not sure where is my mistake: 在我当前的实现中,我遇到以下编译错误,并且不确定我的错误在哪里:

template <int R, int C>
class Matrix {
private:
    double matrix[R][C];
public:
    //C'tor
    Matrix(const double& init = 0){
        for (int i = 0; i < R; i++){
            for (int j = 0; j < C; j++){
                matrix[i][j] = init;
            }
        }
    }

    Matrix(const Initializer& init) {
        for (int i = 0; i < R; i++){
            for (int j = 0; j < C; j++){
                matrix[i][j] = init(i,j);
            }
        }
    }

    //Operators
    double& operator()(const int& i, const int& j){
        return matrix[i][j];
    }

    Matrix<R,C>& operator=(const Matrix<R,C>& otherMatrix){
        for (int i = 0; i < R; i++){
            for (int j = 0; j < C; j++){
                matrix[i][j] = otherMatrix.matrix[i][j];
            }
        }
        return *this;
    }

    Matrix<R,C>& operator+=(const Matrix<R,C>& otherMatrix){
        for (int i = 0; i < R; i++){
            for (int j = 0; j < C; j++){
                matrix[i][j] = otherMatrix.matrix[i][j] + matrix[i][j];
            }
        }
        return *this;
    }

    Matrix<R,C> operator+(const Matrix<R,C>& otherMatrix) const {
        Matrix<R,C> newMatrix;
        newMatrix = otherMatrix;
        newMatrix += *this;
        return newMatrix;
    }
};

q3.cpp:68:16: warning: parentheses were disambiguated as a function declaration [-Wvexing-parse]
        Matrix<1,3> m3(Initializer());
                      ^~~~~~~~~~~~~~~
q3.cpp:68:17: note: add a pair of parentheses to declare a variable
        Matrix<1,3> m3(Initializer());
                       ^
                       (            )
1 warning generated.
Doppelganger:ex4_dry estro$ g++ q3.cpp
q3.cpp:68:16: warning: parentheses were disambiguated as a function declaration [-Wvexing-parse]
        Matrix<1,3> m3(Initializer());
                      ^~~~~~~~~~~~~~~
q3.cpp:68:17: note: add a pair of parentheses to declare a variable
        Matrix<1,3> m3(Initializer());
                       ^
                       (            )
1 warning generated.

The compiler warns you about the most vexing parse . 编译器警告您最烦人的解析 This line : 这行:

Matrix<1,3> m3(Initializer());

Is parsed as a function named m3 that returns a Matrix<1,3> , taking as a parameter an unnamed function with no parameter and returning an Initializer . 被解析为名为m3函数 ,该函数返回Matrix<1,3> ,将不带参数的未命名函数作为参数,并返回Initializer

You can fix it with additional parentheses (as advised by the compiler) : 您可以使用其他括号对其进行修复(如编译器所建议):

Matrix<1,3> m3((Initializer()));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM