class Student
{
int rollNo;
string name;
public:
Student(int id_of_student, string name_of_student)
{
rollNo = id_of_student;
name = name_of_student;
}
void getStudentData()
{
cout<<"The name of the student with roll No. "<<rollNo<<" is "<<name<<endl;
}
};
int main()
{
Student *ptr = new Student[30]; // Error: no default constructor exists for class "Student"
return 0;
}
Is there any way by which we can pass parameters to the constructor?
Error: no default constructor exists for class "Student"
newinstead ofstd::vector? For the latter the problem of creating objects with parameters is somewhat simpler - you can write a loop instead of very long initializer list.emplace_back,newor that C++11 initialization method, there's always the option of the good oldinit-Function. Remove the parameters from the constructor (or supply them with default values), but also define aninitfunction, which does the same thing. Then you can initialize the objects after having allocated the object. Even in a loop if you want.