简体   繁体   English

C++ 模板:“双”错误之前的预期主表达式

[英]C++ Template: Expected primary-expression before 'double' error

When i try to run an instance of the matrix class i'm writing, i get the following error.当我尝试运行我正在编写的矩阵 class 的实例时,出现以下错误。

main.cpp|6|error: expected primary-expression before 'double'| main.cpp|6|错误:'double'之前的预期主表达式|

I believe there's something wrong with the constructor but i can't figure what.我相信构造函数有问题,但我不知道是什么。 How can i fix my code?我怎样才能修复我的代码? Below what i've written so far.低于我到目前为止所写的内容。

matrix.h矩阵.h

#ifndef MATRIX_H
#define MATRIX_H

template<class T>
class matrix{
public:
    matrix(int n, int m);
    ~matrix();
    void fillM(T n);
    void print() const;
private:
    T** body_;
    int lin_;
    int col_;
    void eraseMatrix();
};


#endif

matrix.cpp矩阵.cpp

#include "matrix.h"
#include <iostream>


template<class T>
matrix<T>::matrix(int n, int m):col_(m),lin_(n),body_(new T*[n]){
    for(int i=0;i<n;i++){
        body_[i] = new T[m];
    }
}

template<class T>
matrix<T>::~matrix<T>(){
    eraseMatrix();
}

template<class T>
void matrix<T>::eraseMatrix(){
    for(int i=0;i<lin_;i++){
        delete [] body_[i];
    }
    delete [] body_;
    return;
}

template<class T>
void matrix<T>::fillM(T n){
    int j;
    for(int i = 0;i<lin_;i++){
        for(j = 0;j<col_;j++){
            body_[i][j] = n;
        }
    }
    return;
}

template<class T>
void matrix<T>::print() const{
    int j;
    for(int i=0;i<lin_;i++){
        for(j=0;j<col_;j++){
            std::cout<<body_[i][j]<<" ";
        }
        std::cout<<std::endl;
    }
}

main.cpp主文件

#include <iostream>
#include "matrix.h"
using namespace std;

int main(){
    matrix<double>::matrix<double> A(5,5);//error happens here
    A.fillM(5.2);
    A.print();
    return 0;
}

}

You don't need to write matrix<double>::matrix<double> to construct an instance.您无需编写matrix<double>::matrix<double>来构造实例。 Just matrix<double> A(5,5);只需matrix<double> A(5,5); . .

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

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