Home > Back-end >  "No default constructor exists for class" if I want to construct a class with another one
"No default constructor exists for class" if I want to construct a class with another one

Time:12-14

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 :

#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;
}

CodePudding user response:

The problem is that since you have 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 first 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
}
  •  Tags:  
  • c
  • Related