| | |
intialization of array of objects
![]() |
•
•
Join Date: Nov 2007
Posts: 4
Reputation:
Solved Threads: 0
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!!!
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!!!
Please post using code tags
...
> Change
to
> Edit:: Look at this ...
...> 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."
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
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
...
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.
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: 2 different names of one function
- Next Thread: Conditional template specialization
Views: 321 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C++
6 algorithm array arrays assignment beginner binary browser c++ c++borland c/c++ calculator char class classes code compile compiler constructor conversion convert count delete dll dynamic encryption error file files filestream forms fstream function functions game givemetehcodez graph graphics gui homework iamthwee input int lazy link linker list loop looping loops math matrix member memory newbie number object objects opengl operator output parameter pointer pointers problem program programming project python random read reading recursion recursive reference return server sort spoonfeeding string strings struct student studio template templates text time tree undefined variable vc++ vector video visual win32 window windows winsock wxwidgets






