开发者

Error: expected constructor, destructor, or type conversion before ‘::’ token

I am getting an error when trying to compile my class.

The error:

Matrix.cpp:13: error: expected constructor, destructor, or type conversion before ‘::’ token

Matrix.h

#ifndef _MATRIX_H
#define _MATRIX_H

template <typename T>
class Matrix {
public:
    Matrix();
    ~Matrix();
    void set_dim(int, int);     // Set dimensions of matrix and initializes array
    unsigned int get_rows();    // Get number of rows
    unsigned int get_cols();    // Get number of columns
    void set_row(T*, int);      // Set a specific row with array of type T
    void set_elem(T*, int, int);// Set a specific index in the matrix with value T
    bool is_square();           // Test to see if matrix is square
    Matrix::Matrix add(Matrix); // Add one matrix to another and return new matrix
    Matrix::Matrix mult(Matrix);// Multiply two matrices and return new matrix
    Matrix::Matrix scalar(int); // Multiply entire matrix by number and return new matrix

private:
    unsigned int _rows;         // Number of rows
    unsigned int _cols;         // Number of columns
    T** _matrix;                // Actual matrix data

};

#endif  /* _MATRIX_H */

Matrix.cpp

#include "Matrix.h"
template <typename T>
Matrix<T>::Matrix() {
}

Matrix::~Matrix() { // Line 13
}

main.cpp

#include <stdlib.h>
#include <cstring>
#include "Matrix.h"

int main(int argc, char** argv) {
开发者_如何学JAVA    Matrix<int> m = new Matrix<int>();
    return (EXIT_SUCCESS);
}


Matrix::~Matrix() { } 

Matrix is a class template. You got the constructor definition right; the destructor (and any other member function definitions) need to match.

template <typename T>
Matrix<T>::~Matrix() { }

Matrix<int> m = new Matrix<int>(); 

This won't work. new Matrix<int>() yields a Matrix<int>*, with which you cannot initialize a Matrix<int>. You don't need any initializer here, the following will declare a local variable and call the default constructor:

Matrix<int> m;


Define destructor as

template <typename T>
Matrix<T>::~Matrix() {
}

And other error is that you cannot place implementation of class template in .cpp-file Template Factory Pattern in C++

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜