ARTICLE AD BOX
For starters in you defined a variable length array
cout << "Enter no. of students: "; int n; cin >> n; student students[n];where n is not a constant expression. Variable length arrays are not a standard C++ feature though some compilers can suppot them.
In such a situation it is better to use standard container std::vector instead of an array.
Secondly, when the array is defined then objects of the type student are already created using the default constructor of the class.
So in this statement
students[i](a, b, c);the compiler actually tries to call an operator function that is not defined in the class and as result issues the error message.
You could define the operator function within the class definition for example the following way
void operator ()( const std::string &name, int roll_no, int marks ) { this->name = name; this->roll_no = roll_no; this->marks = marks; }Pay attention to that the first parameter has a constant referenced type (the same should be used in the constructor) and after the operator definition or after a definition of any other member function using semicolon is redundant.
Or instead of using the operator function you could write simply
students[i] = {a, b, c};It looks like you intend to call the constructor of class student. The way to do that in your original code with the non-standard variable-length array is to assign a newly constructed object to the existing object
students[i] = student(a, b, c);or, since the constructor is not explicit, using an assignment from a brace initializer
students[i] = {a, b, c};However, you should actually use a std::vector in place of the variable-length array
std::vector<student> students;and then you can construct the new object in place without an intermediate, temporary object:
students.emplace(a, b, c);14.4k1 gold badge22 silver badges47 bronze badges
The statement students[i](a, b, c); would be a call to an overloaded operator student::operator() (/* 3 arguments */).
student(string a, int b, int c) is a coinstructor. It cannot be called directly. It's a function you call duringcreation of an object of class student.
E.g. you can do create an instance of student and assign it to array element:
students[i] = student{a, b, c}; // also student(a, b, c)But it's not an optimal variant.
Explore related questions
See similar questions with these tags.
