Passing unique_ptr to non-member functions

I am having troble figuring our how to pass around my smart pointer.

I call the function isIdentity on my matrix object h:

void test(const size_t dim)
{
    cout << "identity gen: " << "\t\t" << ((h.isIdentity())?"true":"false") << endl;
}

Here is my matrix class with the relevant functions. It have a member function called isIdentity and a private array called m that contain the actual matrix.

template <typename T> 
class Matrix 
{
private:
    size_t dim;
    unique_ptr<T[]> m;

public:
    explicit Matrix(size_t dim) : dim(dim), m(new T[dim*dim]())
    {
        m = gen_mat<T>(dim); //generates random matrix
    }
    Matrix(T* m, size_t dim):  dim(dim), m(m){} //copy constructor
    ~Matrix(){  
    };

    bool isIdentity()
    {
        return check_identity<T>(this->m,dim);
    }
}

The member function calles this non-member function.

template <typename T> 
bool check_identity(const T *m, size_t dim)
{
    for(size_t i = 0; i < dim; ++i)
    {
        for(size_t j = 0; j < dim; ++j)
        {
            T r = i == j ? m[i*dim+j] - 1.0 : m[i*dim+j];
            if (r < -PRECISION || r > PRECISION)
            {
                return false;
            }
        }
    }
    return true;
}

Question:

For some reason the check_identity function is not recognized. I am very uncertain if my approach to this is the proper way to do it. Can someone explain to me what is wrong and give some guidelines on how to do it right?

Please comment if additional information is required.

Error message:

error: no matching function for call to ‘check_identity(std::unique_ptr<double [], std::default_delete<double []> >&, size_t&)’

Upvotes: 0

Views: 74

Answers (1)

Armen Tsirunyan
Armen Tsirunyan

Reputation: 133112

Your function takes a plain pointer. There is no implicit conversion from a smart pointer to a plain pointer. The explicit conversion is carried out by the member function get

return check_identity<T>(this->m.get(),dim);
                             ///^^^^^

Upvotes: 3

Related Questions