-6

https://repl.it/@IT18117110/MidRevLecEx <<-----Link for the code

This is the error I got
Error: expected unqualified-id before '(' token st[i].Student::(Name_i, id_i);

#include <iostream>
#include "Student.h"
#include "string.h"
using namespace std;

int main() {

  int i = 0;
  char Name_i[20];
  int id_i;
  int mark1_i;
  int mark2_i;
  Student st[3];

  do{

    cout<<"Enter Name:"<<i;
    cin>>Name_i;
    cout<<"Enter ID:";
    cin>>id_i;
    cout<<"Enter Marks1:";
    cin>>mark1_i;
    cout<<"Enter Marks2:";
    cin>>mark2_i;

    st[i].Student::(Name_i, id_i);
    st[i].setMark1(mark1_i);
    st[i].setMark2(mark2_i);
    st[i].printMarks();
    st[i].getAverageMark();
    st[i].~Student();

    i++;
  }while(i < 3);

  return 0;
}
  • Please put all necessary information including code and error messages in the question itself, not using an external link. You can [format code as a code block](https://meta.stackoverflow.com/q/251361/9254539). – eesiraed Aug 20 '18 at 04:39
  • 1
    A constructor is executed, by definition, to initialize an object. Not in any other circumstance. What are you trying to accomplish? – StoryTeller - Unslander Monica Aug 20 '18 at 04:49
  • 1
    Your `Student` constructors get called _implicitly_ when you declare `Student st[3]` and the destructors get called when the same array goes out of scope. It's bad form to call them manually under all but very unusual circumstances. – alter_igel Aug 20 '18 at 04:50
  • If you can't get hold of [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), even the popular online tutorials would be better than whatever you're learning from now. – molbdnilo Aug 20 '18 at 06:52
  • 1
    @NisugaJayawardana Please be careful. Deleting your questions will only aggravate your account's condition to ask further questions. Instead, seek to improve existing questions by editing them if possible. https://meta.stackoverflow.com/questions/255583/what-can-i-do-when-getting-we-are-no-longer-accepting-questions-answers-from-th – E_net4 Oct 17 '21 at 12:32

3 Answers3

0

The default constructor of Student class is already got called when you have created the static instance of your Student class Student st[3]; You can simply write the print statement in the Student class constructor(which doesn't take any argument), you will find that it is getting called 3 times.

As per I know that constructor can't be called explicitly, it is used to construct the object. I think you should declare the pointer of Student class instance instead of declaring static and when you will provide the memory (by calling new operator) at that time you can provide the object initialization parameters value.

Sanjeev
  • 348
  • 2
  • 9
0

When you allocate objects on the stack (in the manner you have done in your example), you should not call constructors or destructors explicitly. Constructor is called at the declaration:

 Student st[3];

and the Destructor is called when the scope is exited.

 return 0;
} <--Here

If you want to set the Name_i, id_i, use setter functions similar to the ones used to set the marks (setMark1 and setMark2).

P.W
  • 26,289
  • 6
  • 39
  • 76
0

You should not attempt to call constructors or destructors explicitly.

Assign a new value to the object in the array, like you would with ints or anything else:

st[i] = Student(Name_i, id_i);

And remove

st[i].~Student();
molbdnilo
  • 64,751
  • 3
  • 43
  • 82