How can I fix the error & ldquo; An object of variable size may not be initialized?


How can I fix the error "Variable-sized object may not be initialized" with the following code:

sv A[i] =new sv(m,t,d,l,tl,ml,nh);

My code from the beginning to the error line:

using namespace std;

class sv{
public: int msv;
  string ten,lop;
  float diem;
  string tenlop, malop;
  int namhoc;
  sv(int m,string t, float d,string l,string tl,string ml, int nh);
class lophoc{
  lophoc(string tl,string ml, int nh); };
  void hienthi(){
  sv::sv(){ }
  sv::sv(int m,string t, float d,string l,string tl,string ml, int nh)
    msv=m; ten=t; diem=d; lop=l; tenlop=tl; malop=ml, namhoc=nh; }
int main(){
  sv A[100];
  int n,i,m,d,nh;
  string t,tl,ml,l;
  cout<<"lan luot nhap ma sv,ten, diem,lop hoc: ";
  cin>> m>>t>>d>>l;
  cout<< "lan luot nhap ten lop, ma lop, nam hoc: ";
  sv A[i] =new sv(m,t,d,l,tl,ml,nh);


sv A[i] =new sv(m,t,d,l,tl,ml,nh);

looks like a declaration of another array of sv called A with run-time size of i to the compiler (A C99 feature). If you wanted to assign to the element at index i, you would do:

A[i] = sv(m,t,d,l,tl,ml,nh);

Note that new allocates on the heap and returns a pointer, which is not required here.