Mohammad Jafar Mashhadi
Mohammad Jafar Mashhadi

Reputation: 4251

Can not define an array of structure in C++

I defined ball structure in this way:

struct ball
{
 _vector coordinates;
 _vector velocity;
 _vector acceleration;

 int border;
 int color;
 int radius;

 float mass;

 void step();
 void clear();
 void render();
};

(data type _vector is defined before and it represents the vector in mathematics)

in the main function i wanted to define an array of balls so i wrote this code:

int main(int argc, char** argv)
{
    struct ball balls[NO_BALLS];
.
.
.
}

but when i wanna compile the code i get this error:

no matching function for call to `ball::ball()' candidates are: ball::ball(const ball&)

Upvotes: 0

Views: 111

Answers (1)

Joseph Mansfield
Joseph Mansfield

Reputation: 110658

If you define the copy constructor ball::ball(const ball&) (which you've actually commented out in your code), there will be no compiler generated defaulted default constructor. There needs to be a default constructor for your array definition to work (because it default initializes each of the elements). So simply provide a default constructor: ball:ball() { }. You will probably want to initialize your member variables in this constructor.

Upvotes: 9

Related Questions