intialization of array of objects

Reply

Join Date: Nov 2007
Posts: 4
Reputation: abhi_iips is an unknown quantity at this point 
Solved Threads: 0
abhi_iips abhi_iips is offline Offline
Newbie Poster

intialization of array of objects

 
0
  #1
Apr 25th, 2009
hi all,

i have created a class names members which looks like this..

class mymembers{
int data;
float dummy;
char* thename;
public :
mymembers()
{
cout<<"the value of i is "<<data;
data =12;
}
mymembers(int ic,char* name)
{
thename = name;
data= ic;
cout<<"hello this object's data value is "<<data<<endl;
}
mymembers(int ic,int it)
{
dummy = 12;
data= ic;
cout<<"hello this object's data value is "<<data<<endl;
}


};

in main i'm initializing an array of 3 objects
the code looks like this...

int main()
{
mymembers m[3] = {(20,22),(21,33),(45,44)};

getch();

}

and i get an error
57 C:\Documents and Settings\abhishek\My Documents\check.cpp conversion from `int' to non-scalar type `mymembers' requested

and if i use one argument constructor to intialize the objects in the array then it shows no problem...so why its not taking the second argument correctly..which conversion it's talking abt...plz help!!!
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 2,034
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 225
tux4life's Avatar
tux4life tux4life is offline Offline
Postaholic

Re: intialization of array of objects

 
0
  #2
Apr 25th, 2009
Please post using code tags ...
> Change
mymembers m[3] = {(20,22),(21,33),(45,44)}; (in your main)
to
mymembers m[3] = {mymembers(20,22),mymembers(21,33),mymembers(45,44)};

> Edit:: Look at this ...
Last edited by tux4life; Apr 25th, 2009 at 1:49 pm.
"You can't build a reputation on what you are going to do."
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 344
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: intialization of array of objects

 
0
  #3
Apr 25th, 2009
Another defects:
1. the default (the 1st) ctor does not initialize dummy and thename members.
2. the 2nd ctor does not initialize dummy member. It saves a simple pointer to the 2nd arg (especially dangerous)
3. the 3rd ctor does not initialize name member (especially dangerous case). Can't get literal names (char* instead of const char* ).
4. mymembers(x,0) ctor calls are ambiguous w/o explicit cast.
5. No copy ctor and assignment overloading for the class with pointer members (as usually it's a very dangerous symptom).

Difficult class confinement ...
Last edited by ArkM; Apr 25th, 2009 at 6:31 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 321 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC