array of objects

A

AHR

Hi NG

I have a class which contains:
**********************
class felt
{
private:
int id;
int koorx;
int koory;
public:
felt(int id_x, int koorx_x, int koory_x)
{
id=id_x;
koorx=koorx_x;
koory=koory_x;
}
};
**********************

Then I have my main which should contain an array of felt-objects:
**********************
int main()
{
//Example
felt nr1(1,1,1);

??? arr[4];
arr[0]=???;
arr[1]=???;
arr[2]=???;
arr[3]=???;

return 0;
}
**********************

But how do i declare an object array? Please help me to insert the right
values on the questionmarks.

Thanks
AHR
 
M

Mike Wahler

AHR said:
Hi NG

I have a class which contains:
**********************
class felt
{
private:
int id;
int koorx;
int koory;
public:
felt(int id_x, int koorx_x, int koory_x)
{
id=id_x;
koorx=koorx_x;
koory=koory_x;
}

I suggest using an intitalization list for this:

felt(int id_x, int koorx_x, int koory_x)
: id(id_x), koorx(koorx_x), koory(koory_x)
{
}

More below.
**********************

Then I have my main which should contain an array of felt-objects:
**********************
int main()
{
//Example
felt nr1(1,1,1);

??? arr[4];

felt arr[4];

But wait, this isn't quite enought. If you want to create
an array of user defined type, this type must be 'default-
constructible', i.e. it needs a default constructor, which
your 'felt' class does not have. You can either supply default
parameters for your contstructor, or create another constructor
which gives the data members some sensible default value (perhaps
zero?). (A 'default' constructor is one which can be invoked
with no arguments.

An example default constructor:
'
felt()
: id(0), koorx(0), koory(0)
{
}

Once you have this, you can create your array such as:

felt arr[4];
arr[0]=???;
arr[1]=???;
arr[2]=???;
arr[3]=???;

arr[0] = nr1;

return 0;
}
**********************

But how do i declare an object array? Please help me to insert the right
values on the questionmarks.

Since you have not defined any other conversions, the only thing you
can put on the right side of those assignment operators is an expression
of type 'felt', such as the name of your object 'nr1', or perhaps a
temporary object such as 'felt(1, 2, 3)'.

Having said all that, I suggest you look into using a standard container
such as 'std::vector',. which will handle all memory management for you
automatically (e.g. it can grow and shrink as needed to accomodate the
elements you want to store, this is not the case with an array).

Which C++ book(s) are you reading?

-Mike
 
V

Victor Bazarov

Mike Wahler said:
AHR said:
Hi NG

I have a class which contains:
**********************
class felt
{
private:
int id;
int koorx;
int koory;
public:
felt(int id_x, int koorx_x, int koory_x)
{
id=id_x;
koorx=koorx_x;
koory=koory_x;
}

I suggest using an intitalization list for this:

felt(int id_x, int koorx_x, int koory_x)
: id(id_x), koorx(koorx_x), koory(koory_x)
{
}

More below.
**********************

Then I have my main which should contain an array of felt-objects:
**********************
int main()
{
//Example
felt nr1(1,1,1);

??? arr[4];

felt arr[4];

But wait, this isn't quite enought. If you want to create
an array of user defined type, this type must be 'default-
constructible', i.e. it needs a default constructor, which
your 'felt' class does not have.

That's not necessarily so. Even if there is no default c-tor, you
can construct an array if you can supply all the initialisers for
the elements:

felt arr[] = { felt(1,1,1), felt(2,2,2), felt(3,3,3), felt(4,4,4) };

In this case the only requirement is that 'felt' is copy-constructible.

Victor
 

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

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,949
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top