weird constructor error

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?

Thank you

heres a link to the code+complete error
http://ideone.com/G939CF

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

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?
star::star(const star&) is the implicit constructor, like star newStar= star()

You need to add a default blank constructor... try adding this constructor to your star class.

1
2
3
4
5
star()
{
    x=0;
    y=0;
}


Your right about the not using it as a name... I write VHDL most of the time and occasionally get confused... sorry.
Last edited on
Ok. Thanks:)
Line 25 in the OP's code is a function declaration.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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 ;
}


http://ideone.com/myzvkn
Last edited on
Topic archived. No new replies allowed.