Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

"No default constructor exists for class" if I want to construct a class with another one

I have a class named Ball and I want to create a class Particle with, in its constructor, a std::vector<Ball> particles, but I’m getting the error "No default constructor exists for class ‘Ball’"

The Ball.h file :

#pragma once
#include <vector>
#include <raylib.h>


class Ball {
    private:
        Vector3 position;
        Vector3 speed;
        float radius;
    public:
        Ball(Vector3 ballPosition, Vector3 ballSpeed,float ballRadius);       
};

The Ball.cpp file :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#include "Ball.h"
#include <raymath.h>
#include <rlgl.h>

Ball::Ball(Vector3 position, Vector3 speed,float radius) {
    this->position = position;
    this->speed = speed;
    this->radius = radius;

}

The Particles.h file :

#pragma once
#include <vector>
#include <raylib.h>
#include <Ball.h>

class Particles{
    private:
        std::vector<Ball> particles;
        std::vector<Vector3> velocities;

        float w;
        float c1;
        float c2;

    public:
        Particles(std::vector<Ball> particles, std::vector<Vector3> velocities, float w, float c1, float c2);
};

The Particles.cpp file (where the error is):

#include "Ball.h"
#include <raymath.h>
#include <rlgl.h>
#include "Particles.h"


Particles::Particles(std::vector<Ball> particles, std::vector<Vector3> velocities, float w, float c1, float c2) {
    this->particles = particles;
    this->velocities = velocities;
    this->w = w;
    this->c1 = c1;
    this->c2 = c2;
}

>Solution :

The problem is that since you have defined a parameterized constructor for your class Ball, the compiler will not synthesize a default constructor by itself. So if you want to create/construct an object of class Ball using a default constructor, say by writing Ball b;, then you must add/provide a default constrcutor for class Ball as shown below.

Solution 1

Just add a default constrctor in class Ball as shown below:

Ball.h

class Ball {
    private:
        Vector3 position;
        Vector3 speed;
        float radius = 0;//use in-class initializer
    public:
        Ball(Vector3 ballPosition, Vector3 ballSpeed,float ballRadius); 
        Ball() = default;   //DEFAULT CONSTRUCTOR ADDED   
};

Alternative Method of adding constructor

Another way of adding a default constructor would be as shown below:

Ball.h

#pragma once
#include <vector>
#include <raylib.h>


class Ball {
    private:
        Vector3 position;
        Vector3 speed;
        float radius = 0;//use IN-CLASS INITIALIZER
    public:
        Ball(Vector3 ballPosition, Vector3 ballSpeed,float ballRadius); 
        Ball(); //declaration for default constrctor      
};

Ball.cpp

#include "Ball.h"
#include <raymath.h>
#include <rlgl.h>

Ball::Ball(Vector3 position, Vector3 speed,float radius) {
    this->position = position;
    this->speed = speed;
    this->radius = radius;

}
//define the default constructor
Ball::Ball()
{
    cout << "default constructor" << endl;
    //do other things here if needed
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading