Unable to access private member declared in class 'std::tr1::random_device'

I have
class CRandom
{
.
.
.

private:
   std::random_device m_rd;
.
.
.
};


but I get C2248 errors whenever I try to pass the object to anything (and compiler generates CRandom::CRandom(const CRandom &)
error is
  vc\include\random(6438) see decl of 'std::tr1::random_device::random_device'
  vc\include\random(6408) see decl of 'std::tr1::random_device'
The diag occurred in the compiler generated function 'CRandom::CRandom(const CRandom &)

I tried to write my own overloaded assignment operator but that doesn't seem to help
LVL 1
supportorangesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
>>I tried to write my own overloaded assignment operator but that doesn't seem to help

That - plus providing a copy constructor - usually the way to go, e.g.

class CRandom
{
public:

  CRandom(const CRandom& r)
  {
    Copy(r);
  }
  CRandom& operator=(const CRandom& r)
  {
    Copy(r);

    return *this;
  }
protected:

  void Copy(const CRandom& r)
  {
    m_rd = r.m_rd;
    // copy other stuff as well
  }

private:
   std::random_device m_rd;

};

Open in new window

0
supportorangesAuthor Commented:
Thank you..  You are right about creating copy ctor and overloaded assignment operator and I tried, but I can't get to m_rd because of the way it is declared in vc\include\random .    the line trying to access m_rd would not compile.

"Cannot access priate member declared in  class 'std::tr1::random_device'
0
supportorangesAuthor Commented:
before i tried overloaded assignment and copy ctor i got:

this diagnostic occurred in the compiler generated function CRandom::CRandom(const CRandom &)

but writing my own didn't change the diagnostic was is

...random_device' :  C2248 cannot access private member declared in std::tr1::random_device and
..\include\random(6438) see decl of 'std::tr1::random_device::random_devie'
..\include\random(6408) see decl of 'std::tr1::random_device'
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

supportorangesAuthor Commented:
CRandom& operator =(const CRandom& r)
{
    m_rd = r.m_rd;  //generates C2248 'std::tr1::random_device::operator = ' : cannot access private mbr in 'std::tr1:random_device
//other ok statements
.
.

}
0
jkrCommented:
Aaah, got it - 'random_device' is not supposed to be copyable. Thus, the assignment operator and copy constructor is declared as private:

class random_device
	{	// class to generate random numbers (from hardware where available)
public:
	typedef unsigned int result_type;

	explicit random_device(const string& = "")
		{	// construct
		(*this)();	// force early failure if bad engine
		}

	static result_type (min)()
		{	// return minimum possible generated value
		return (0);
		}

	static result_type (max)()
		{	// return maximum possible generated value
		return ((result_type)-1);
		}

	double entropy() _NOEXCEPT
		{	// return entropy of random number source
		return (32.0);
		}

	result_type operator()()
		{	// return next value
		return (_Random_device());
		}

private:
	random_device(const random_device&);	// not defined
	random_device& operator=(const random_device&);	// not defined
	};

Open in new window


Are you sure you need that as a class member? It usually is used as a local variable, see e.g. http://msdn.microsoft.com/en-us/library/bb982398.aspx ("<random>")
0
jkrCommented:
Sorry, that was badly worded, let me try again:

'random_device' is not supposed to be copied. Thus, the assignment operator and copy constructor is declared as private to make it uncopyable.

The options you have now are

- don't make it a class' member, since that will render the entire class uncopyable, use it as a local variable in the methods that need it
- derive from 'random_device' and provide an assignment operator and copy ctor. Could work, but there usually is a reason why they made it that way.
- edit '<random>' and remove the private members. Not recommended at all.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
supportorangesAuthor Commented:
Thank you for the well thought out and well-articulated response.  

I changed the generator to look like this:

int CRandom::generator(int nMin, int nMax)
{
      std::random_device rd; //use expert suggestion
      //now use rd
}

All the problems have gone away!  Thank you so much.  It is really appreciated.
0
supportorangesAuthor Commented:
So awesome I don't have to mess with my design!
0
jkrCommented:
Great to know that it helped ;o)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.