By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,743 Members | 1,058 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,743 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Aug 28 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Aug 28, 9:54 pm, kkirtac <kadir.kir...@gmail.comwrote:
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

Aug 28 '07 #2

P: n/a
On 2007-08-28 18:54, kkirtac wrote:
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.

--
Erik Wikström
Aug 28 '07 #3

P: n/a
On Aug 28, 8:13 pm, Reetesh Mukul <reetesh.mu...@gmail.comwrote:
On Aug 28, 9:54 pm, kkirtac <kadir.kir...@gmail.comwrote:
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
Thank you for replies, you are both right, i dont have a default
constructor..thanks again

Aug 28 '07 #4

P: n/a
kkirtac <ka**********@gmail.comwrote in news:1188320088.558946.59700
@g4g2000hsf.googlegroups.com:
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, myClassfunc()
{
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
Aug 28 '07 #5

P: n/a
On Tue, 28 Aug 2007 17:13:19 +0000, Reetesh Mukul wrote:
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

--
SasQ
Aug 29 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.