简体   繁体   中英

C++ illegal member initialization in template class

i have looked through some similar questions about illegal member initialization in c++ with the same error C2614, but it seems is not the same with my problem.
I have a template class 'Mat2D':

#pragma once

#include <iostream>
#include <iomanip>
#include <assert.h>
template<class T>
class Mat2D
{
private:
    int _rows;
    int _cols;
    T** _data;

public:
    Mat2D()
        :_rows(0), _cols(0), _data(NULL)
    {

    }

    Mat2D(const Mat2D<T>& rhs)
        :_rows(rhs._rows), _cols(rhs._cols)
    {
        cloneData(rhs._data);
    }

    Mat2D(int rows, int cols)
    {
        _rows = rows < 1 ? 1 : rows;
        _cols = cols < 1 ? 1 : cols;
        allocateData();
    }

    Mat2D(int rows, int cols, const T& initValue)
        :Mat2D(rows, cols) //--> error C2614:  'Mat2D<T>' : illegal member initialization: 'Mat2D<int>' is not a base or member 

    {
        all(initValue);
    }

    ~Mat2D()
    {
        for(int i = 0; i < _rows; i++)
            delete _data[i];
        delete _data;
    }
//...
};
Mat2D(int rows, int cols, const T& initValue)
    :Mat2D(rows, cols)

This probably won't work since initialization lists are for variables only. Try with

Mat2D(int rows, int cols, const T& initValue)
    :_rows(rows), _cols(cols)

or copy the contents of the Mat2D(int cols, int rows) constructor in this function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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