So I'm trying to make a class (called 'collection') which will have an array of many objects of a different class(called 'star'), but I'm getting an error on the line where i define the constructor for the 'collection' class, saying I've got too few arguments to call the star constructor. I don't mean to be calling the star constructor.. What do I not understand/ have I done wrong?
If i delete the contents of the collection constructor, the error goes away.. Again, why?
class star
{
public:
int x;
int y;
star(int xin, int yin)
{
x=xin;
y=yin;
}
};
class collection
{
public:
int x;
collection()
{
x=0;
}
star starArray[20];
};
collection CollOfStars();
int main()
{
return 0;
}
you need a default constructor for your star class
Line 22 star starArray[20]; for each element of the star array the star default constructor is called but you don't have one.
Tester> g++ ./test.c -o test
./test.c: In constructor 'collection::collection()':
./test.c:22: error: no matching function for call to 'star::star()'
./test.c:10: note: candidates are: star::star(int, int)
./test.c:6: note: star::star(const star&)
Looks like you've declared a class called star then tried to use it as a name... bad idea.
Oh. Ok, thanks
is there a reason it gave me the error on the collection constructor line though?
But ValliusDax I haven't used it as a name anywhere.. Can you elaborate? i only use the word star to name the star class, define its constructor, and then define an array of star objects..
I don't understand where its getting the "star::star(const star&)" code from. Is that like what it's expecting the default constructor to be, were it to exist?
struct star
{
int x ;
int y ;
star() = default ;
star(const star&) = default ;
star(int _x, int _y) :x(_x), y(_y) {}
};
struct collection
{
public:
int x = 0;
star starArray[20];
};
int main()
{
collection collOfStars ;
}