错误:'template'之前的预期unqualified-id

时间:2017-11-05 13:39:57

标签: c++

你能帮我吗?我得到这个错误:在c ++之前预期'template'之前的unqualified-id。

Matrix.h:

#ifndef MATRIX_H_INCLUDED
#define MATRIX_H_INCLUDED
#include <iostream>

using namespace std;

template <typename T>
class Matrix {
    int     m;
    int     n;
    T   **data;

public:
    void    newData(int, int);
}

#endif

Matrix.cpp:

#include "Matrix.h"


template <typename T>    // here is the error
void    Matrix<T>::newData(int m, int n) {
    int     i;
    int     j;

    this->m = 0;
    this->n = 0;
    this->data = NULL;
    if (m <= 0 || n <= 0)
        return;
    this->data = new T*[m];
    for (i = 0; i < m; i++)
    {
        this->data[i] = new T[n];
        for (j = 0; j < n; j++)
            this->data[i][j] = 0;
    }
    this->m = m;
    this->n = n;
}

编译器想告诉我什么?

如何修复这些错误?

1 个答案:

答案 0 :(得分:1)

你在课程结束时忘记了;

template <typename T>
class Matrix {
    int     m;
    int     n;
    T   **data;

public:
    void    newData(int, int);
}; // <------------------------- Here