mardi 1 novembre 2016

Call to implicitly-deleted copy constructor of RandGenerator

I have a class that creates an object to produce random numbers in a certain range, the code looks like this

#pragma once

#include <random>

class RandGenerator
{
public:
    RandGenerator();
    RandGenerator(const float& min, const float& max);
    float nextRandom();

private:
    std::random_device mRandDevice;
    std::mt19937 mGenerator;
    std::uniform_real_distribution<> mSampler;
};

RandGenerator::RandGenerator() : RandGenerator(0.f, 1.f)
{}

RandGenerator::RandGenerator(const float& min, const float& max)
{
    mGenerator = std::mt19937(mRandDevice());
    mSampler = std::uniform_real_distribution<>(min, max);
}

float RandGenerator::nextRandom()
{
    return mSampler(mGenerator);
}

Then, I have created another class that has an instance of RandGenerator to sample from the unit sphere (pretty simple and naive but just for learning purposes), which looks like

#pragma once

#include "randgenerator.h"
#include "point.h"

class UnitSphereSampler
{
public:
    UnitSphereSampler();
    Point nextSample();

private:
    RandGenerator mRandGenerator;
};

UnitSphereSampler::UnitSphereSampler() :
mRandGenerator(RandGenerator(-1.f, 1.f)) // ERROR HERE
{
}

Point UnitSphereSampler::nextSample()
{
    Point p;

    do
    {
        float x = mRandGenerator.nextRandom();
        float y = mRandGenerator.nextRandom();
        float z = mRandGenerator.nextRandom();
        p = Point(x, y, z);
    } while ((p - Point(0.f)).normSq() >= 1.f);

    return p;
}

from what I can see in this c++ reference the object std::random_device does not have copy constructor, that's why I am using mRandGenerator(RandGenerator(-1.f, 1.f)) in the constructor of the UnitSphereSampler class.

This worked fine in VS2013 however, I tried to compile in OS X Sierra and got the error

Call to implicitly-deleted copy constructor of RandGenerator

I have tried to pin point the place where the copy is being done but I cannot seem to find it, am I looking for the right thing or is there something else I am missing? I thought the way I am initializing the RandGenerator avoids doing a call to the copy constructor. Any advise?

Aucun commentaire:

Enregistrer un commentaire