473,383 Members | 1,801 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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

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
5 1572
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Patrick | last post by:
I am investigating a web-based app wherein I wanted to provide html form frontends to an Excel spreadsheet sitting on a MS Windows server; with each authenticated HTTP user having thier own...
3
by: ram | last post by:
I have a user control uc (with a dropdownlist uc_dd) that I dynamically add to a pane on a button_click. this pane is hosted on the winform. now when I add two instances of the user control...
1
by: Johan | last post by:
If a user happens to open multiple instances of her frontend, what kind of problems can occur? For example, one user reported that she was able to, once or twice, bypass the evaluation...
0
by: clintonG | last post by:
I applied aspnet_regsql to SQL2K which was working fine throughout Beta 2 development. After installing Visual Studio and SQL Express RTM my application has blown up. Logging in to the application...
6
by: | last post by:
I have made some user controls with custom properties. I can set those properties on instances of my user controls, and I have programmed my user control to do useful visual things in response to...
6
by: =?Utf-8?B?Um9nZWxpbw==?= | last post by:
hey, I wanted to programatically create a different instance on my local computer instead of (local)\SQLEXPRESS I want the user to be able to type in an instance, and it will be created. I 've...
3
by: R.A.M. | last post by:
Hello, I have created ASP.NET application with aspnetdb database. In the following codeI have an error: System.Security.Principal.IPrincipal user = HttpContext.Current.User; if...
5
by: Neil | last post by:
"lyle" <lyle.fairfield@gmail.comwrote in message news:48c3dde7-07bd-48b8-91c3-e157b703f92b@f3g2000hsg.googlegroups.com... Question for you. I'm doing something similar, only, instead of opening...
1
by: R.A.M. | last post by:
Hello, I have started programming ASP.NET application using SQL Server 2005 Express Edition. The problem is that when C# code tries to access aspnetdb database, for example:...
0
by: jllanten | last post by:
I will appreciate any help you can provide me. In the company where i work we have a project which creates about 4-5M records daily of stats. We're currently storing this data in a db named...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.