how to return 2 instances of the same user-defined class from a function

K

kkirtac

Hi, i have a class "myClass", and i want to return two instances of
the same class from a function. I define an array as follows :
"myClass sample[2] ;" and i initialize two instances: "myClass
sample1, sample2 ;"
"sample[0] = sample1 ; sample[1] = sample2 ; return sample ;" This
caused the error :
error C2512 : 'myClass' : no appropriate default constructor available

any idea how to return 2 instances and what is wrong with this
implementation..

Regards
 
R

Reetesh Mukul

Hi, i have a class "myClass", and i want to return two instances of
the same class from a function. I define an array as follows :
"myClass sample[2] ;" and i initialize two instances: "myClass
sample1, sample2 ;"
"sample[0] = sample1 ; sample[1] = sample2 ; return sample ;" This
caused the error :
error C2512 : 'myClass' : no appropriate default constructor available

any idea how to return 2 instances and what is wrong with this
implementation..

Regards

When you are writing myClass sample[2], two instances of myClass are
created at this point only. However, each of these objects required
default constructor myClass(void). Perhaps your myClass does not have
default constructor ...

Regards,
RM
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Hi, i have a class "myClass", and i want to return two instances of
the same class from a function. I define an array as follows :
"myClass sample[2] ;" and i initialize two instances: "myClass
sample1, sample2 ;"
"sample[0] = sample1 ; sample[1] = sample2 ; return sample ;" This
caused the error :
error C2512 : 'myClass' : no appropriate default constructor available

When you declare the array

myClass sample[2];

both elements of that array will have to be initialised, which means
calling the default constructor. Your class does not have a default
constructor hence the error. You can initialise the array with the
correct values from the beginning like this:

myClass sample[2] = { sample1, sample2 };
any idea how to return 2 instances and what is wrong with this
implementation..

To find out how to return two values from a function read the archives,
the question has been answered two times in less than a week.
 
K

kkirtac

Hi, i have a class "myClass", and i want to return two instances of
the same class from a function. I define an array as follows :
"myClass sample[2] ;" and i initialize two instances: "myClass
sample1, sample2 ;"
"sample[0] = sample1 ; sample[1] = sample2 ; return sample ;" This
caused the error :
error C2512 : 'myClass' : no appropriate default constructor available
any idea how to return 2 instances and what is wrong with this
implementation..

When you are writing myClass sample[2], two instances of myClass are
created at this point only. However, each of these objects required
default constructor myClass(void). Perhaps your myClass does not have
default constructor ...

Regards,
RM

Thank you for replies, you are both right, i dont have a default
constructor..thanks again
 
J

Joe Greer

Hi, i have a class "myClass", and i want to return two instances of
the same class from a function. I define an array as follows :
"myClass sample[2] ;" and i initialize two instances: "myClass
sample1, sample2 ;"
"sample[0] = sample1 ; sample[1] = sample2 ; return sample ;" This
caused the error :
error C2512 : 'myClass' : no appropriate default constructor available

any idea how to return 2 instances and what is wrong with this
implementation..

Regards

I am guessing that the problem is that when you declare an array like:

myClass arr[2];

It actually will default construct two instances of myClass. The
message sounds like you don't have a default constructor.

The second problem you will have with an array is that when you return
one, it automatically converts the array reference into a pointer.
Sadly, this pointer will point to memory that is local to the function
and will therefore be invalid after the return.

There are a lot of ways to return two instances to the caller. The
easiest is to have the caller pass into you an array of pointers to
myClass or a struct with two pointers:

void func(myClass ** ppRet)
{
ppRet[0] = new myClass;
ppRet[1] = new myClass;
}

-or-

struct MyRet {
myClass * pFirst;
myClass * pSecond;
};

void func(MyRet & ret)
{
ret.pFirst = new myClass;
ret.pSecond = new myClass;
}

Alternately, you can make use to std::pair<> and construct the returns:

std::pair<myClass *, myClass *> func()
{
return std::make_pair(new myClass, new myClass);
}

or if myClass is more value oriented:

std::pair<myClass, myClass> func()
{
return std::make_pair(myClass(), myClass());
}

If you have access to boost or TR1, you can look at boost::tuple to get
more perl-like multiple return value support.

Like I said earlier, there are lots of ways to do this, your imagination
is the limiting factor. The key things to remember is that if you
construct something on the stack in func, then you will have to copy it
to it's target and the target will usually be default constructed. If
you create something on the heap with new, then you are talking about
passing pointers around and not the class itself.

Hope that helps some,
joe
 
S

SasQ

When you are writing myClass sample[2], two instances of
myClass are created at this point only. However, each of
these objects required default constructor myClass(void).

It doesn't require. It can be done by using
collective initialization, like the following:

myClass sample[2] = {
myClass(param1,param2,param3...),
myClass(param1,param2,param3...)
};

The default constructor is required only if there isn't
any initializer:

myClass sample[2]; //no initializer, default ctor required
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top