Hi everybody,
I want to declare a class having an object of another class as its member data. but I recevie compiler error: "No appropriate default constrctor available"
my code samlpe is as follows:

class bigInt
{
    bigInt(unsigned);
    bigInt(const bigInt &);
    
   unsigned *value
}
class RSA
{
   RSA(unsigned);
   bigInt n;
}
RSA::RSA(unsigned m)
{
   bigInt n(m);
}

When I add default constructor to bigInt this problem is solved but object n is not created accurately and I got runtime exception while accessing n.
What is my fault?
Thanks in advnce

When I add default constructor to bigInt this problem is solved

OK, that problem is presumably solved.

but object n is not created accurately and I got runtime exception while accessing n.
What is my fault?

I'm guessing that you are using the pointer value uninitialized/unallocated. Couldn't you change that to a simple: unsigned value; You might also post the actual code that's giving you the error.

Value is initialized correctly in bigInt constructor and it is successfully accessed out of RSA class. Problem occures only when i try to have a bigInt object in another class. bigInt costructor and copy constructor are as follows:

bigInt::bigInt(unsigned len)
{
	length = len;
	value = new WORD_TYPE[len];
	for (int i = 0; i < length; i++)
		value[i] = 0;
}

bigInt::bigInt(const bigInt &n)
{
	length = n.length;
	value = new WORD_TYPE[length];
	for (int i = 0; i < length; i++)
		value[i] = n.value[i];
}

The constructors are not PUBLIC.

What compiler are you using? Did it produce any warnings or errors that you failed to correct? You should treat all warnings as if they are errors because most of the time warnings are indeed errors.

Of course, bigInt constructors must be public. But true reason of your troubles is incorrect RSA constructor. That's a right form:

RSA::RSA(unsigned m):n(m)
{};

And no need in default constructor...

More serious remark: presented RSA constructor does not initialize its bigInt member at all!
See what happens:

RSA::RSA(unsigned m)
{
   bigInt n(m);
}

Remember: a constructor is a (special, of course, but only) function. This function creates LOCAL variable n (it's not a member n!) then discard it when finished. So RSA member n wants a default constructor (w/o arguments). But no implicit default constructor if a class has user-defined constructor...

That's all, folks!..

Oh, yet another remark. RSA::RSA(unsigned) defines implicit conversion from unsigned to RSA. Obviously, it's unwanted side effect in this case. Don't forget to add a magic word explicit in this constructor declaration:

class RSA
{
   explicit RSA(unsigned);
   bigInt n;
};

No this useless (may be dangerous) convertion now...

Good luck!

Thanks a lot ArkM!
You solved my problem. But i didn't get your second remark. What is the usage of explicit.
Thanks again.

Thanks lot ArkM!
You solved my problem. But i didn't get your second remrk. What is the usge of explicit?
Thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.