Declaring array of class object with constructor

May 14, 2013 at 7:46pm
Is it possible to create an array of objects from a class with a constructor that requires input.
E.g

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
class Ship
{
private:
int SXCoord;
int SYCoord;
public:
Ship(int x , int y):SXCoord(x),SYCoord(y){}
};

class Physics
{
private:
int PlayerPos;
int SYCoord;
Ship  AllShips[2];
public:
Physics():PlayerPos(1){}
};


The Ship array cant be created because it says

error: no matching function for call to 'Ship::Ship()'|

which i am guessing is because i don't give it any parameters.
But then if i try an assign parameters then it says
error: 'AllShips' declared as function returning an array|


I was just wondering if its possible ?
May 14, 2013 at 7:50pm
I've not actually tried it but i know it must be done in the constructor.

Aceix.
May 14, 2013 at 7:54pm
Yeah i tried that as well but it still just says no matching function call.
Either that or i'm doing it wrong.
Would it be possible for you to post working code showing this ?
May 14, 2013 at 10:47pm
?
May 14, 2013 at 10:56pm
The compiler maybe thought you were attempting to declare a function with AllShips[]() ?
Did you do this:
 
Ship Allships[2] (int,int);  //Function prototype mixed with a variable 


Making AllShips a pointer instead of an array with a fixed size would probably solve two problems:
1) Unlimited size because in the constructor you can dynamically allocate space for Ships* Allships
2) Now you don't have to make a Ships() default constructor because(besides making Allships static) you would have to set all the ships with the same coordinates

1
2
3
4
5
6
7
Ship* Allships;
//...
Physics::Physics(int newpopulation):PlayerPos(1){
   if(newpopulation > 0)
       Allships = new ship(newpopulation)
   else   Allships = nullptr;
}

Of course, if you follow my tip, you'd have to make sure you deallocate the memory used.
May 14, 2013 at 11:07pm
Hi thanks for the reply :)
When you asked me if i did that, were you saying that was wrong ?

Also i knew about the second way but i just prefer not to dynamically allocate memory if its not needed as i will have a set amount of ships.

I also realise that i could make the constructor set the points to zero and then input the coords in a similar way to your pointer method.

Is there no way ,if params MUST be put in for the ship constructor, to create an array of Ships ?
May 14, 2013 at 11:16pm
Yes, that is wrong as you are mixing variable and function syntax. Allships can only be one or the other.

In the Physics constructor, you can initialize Allships via initializer list. This will assign values to Allships as it is created, not after.
1
2
3
4
5
6
Physics():
   PlayerPos(1),
   Allships[0](1,1), //Don't hold this to be true; I don't actually
   Allships[1](10,10)//   know how to initialize member object arrays, 
                  //and this is a guess
{}
May 14, 2013 at 11:29pm
Hey,
That still doesn't work for me and after looking around other places as well i think it might not be possible to do it the way i was thinking.

Instead i'm just going to create a function that sets the coords after it has been initialised with everything at 0.

Thanks again for the help :)
Topic archived. No new replies allowed.