SeekHunt1334 SeekHunt1334 - 23 days ago 17
C++ Question

Dynamically Allocated mpfr_t matrix with std::vector

I want to have a class that works as a matrix of mpfr_t elements. I thought STL Vectors would be a great idea for dynamically allocating as many of these as I want, but I get some errors here.

#ifndef GMPMATRIX_H
#define GMPMATRIX_H

#include <vector>
#include <mpfr.h>

typedef std::vector<mpfr_t> mpVector;
typedef std::vector<mpVector> mpMatrix;

class GmpMatrix
{
private:
int n;
int m;
mpMatrix elements;

public:
GmpMatrix() {}
GmpMatrix(int n, int m)
{
this->n = n;
this->m = m;

for (int i = 0; i < n; ++i)
{
mpVector e_push(m);
for (int j = 0; j < m; ++i)
{
mpfr_t e;
mpfr_init(e);
e_push.push_back(e);
}
}
}


~GmpMatrix() {}
};

#endif // GMPMATRIX_H


The errors are:

/usr/include/c++/5/ext/new_allocator.h:120:4: error: parenthesized initializer in array new [-fpermissive]
{ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
^
/usr/include/c++/5/ext/new_allocator.h:120:4: error: no matching function for call to ‘__mpfr_struct::__mpfr_struct(const __mpfr_struct [1])’


I've really looked into this and I can't figure it out. Is there a way to make a
vector< vector<mpfr_t> >
work? Does anyone know what is happening?

Answer

mpfr_t is a C-style array type. You cannot store those in std::vector.

You can wrap mpfr_t in a simple struct and store those.

Comments