Write a dataset to HDF5 using a user defined Class (C++)


My data set is an N x N matrix and it represents a scalar field.

The matrix representation is done by defining my own scalar field class. This way, I can avoid double pointers and I can write my data in a contiguous chunk of memory. Hence, FIELD(i,j) translates to FIELD[i + N*j].

Now, I wrote the following code to write some data to HDF5 (a matrix full of twos).

#include "H5Cpp.h"
#include "Mesh2D.h"

using namespace H5;

int main()

  int Nx = 10;
  int Ny = 10;
  Mesh2D HFIELD{Nx,Ny,0};

  //Create HDF5 file//
  const H5std_string FILE_NAME("EXAMPLE_DSET.h5");
  H5File file(FILE_NAME, H5F_ACC_TRUNC);

  //Create the data space for the data set//
  hsize_t dims[2];
  const int RANK = 2;
  dims[0] = Nx;
  dims[1] = Ny;
  DataSpace dataspace(RANK,dims);

  //Create the dataset//
  const H5std_string DATASET_NAME("dset");
  DataSet dataset = file.createDataSet(DATASET_NAME, PredType::NATIVE_DOUBLE, dataspace);

  //Write the data to the dataset using default memory space, file space and transfer properties//
  dataset.write(HFIELD, PredType::NATIVE_DOUBLE);
  return 0;

However, I am getting the following error:

error: no matching function for call to ‘H5::DataSet::write(Mesh2D&, const H5::PredType&)’
   34 |   dataset.write(HFIELD, PredType::NATIVE_DOUBLE);

How can I overcome this problem?

EDIT: Note that the memory allocation inside the 2D mesh (scalar field) class constructor is done using the fftw_malloc function of the FFTW3 library.