[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2014-08-03
9
Medium Priority
?
515 Views
Last Modified: 2014-08-03
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
0
Comment
Question by:supportoranges
  • 5
  • 4
9 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 40237646
>>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
 
LVL 1

Author Comment

by:supportoranges
ID: 40237655
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
 
LVL 1

Author Comment

by:supportoranges
ID: 40237663
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:supportoranges
ID: 40237685
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
 
LVL 86

Expert Comment

by:jkr
ID: 40237710
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
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 40237724
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
 
LVL 1

Author Comment

by:supportoranges
ID: 40237859
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
 
LVL 1

Author Closing Comment

by:supportoranges
ID: 40237862
So awesome I don't have to mess with my design!
0
 
LVL 86

Expert Comment

by:jkr
ID: 40238171
Great to know that it helped ;o)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

826 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question