如何修复错误" Variable-sized object may not be initialized "?



如何用以下代码修复"可变大小对象可能未初始化"的错误:

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

我的代码从开始到错误行:

#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
class sv{ 
public: int msv; 
  string ten,lop; 
  float diem; 
  string tenlop, malop; 
  int namhoc; 
  sv(); 
  sv(int m,string t, float d,string l,string tl,string ml, int nh);
class lophoc{ 
public:
  lophoc(); 
  lophoc(string tl,string ml, int nh); };
  void hienthi(){
  cout<<msv<<"t"<<ten<<"t"<<diem<<"t"<<tenlop<<"t"<<malop<<"t"<<namhoc<<"n";
            }
 }; 
  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; 
  cin>>n; 
  for(i=1;i<=n;i++){
  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: "; 
  cin>>tl>>ml>>nh; 
  sv A[i] =new sv(m,t,d,l,tl,ml,nh);
}
}

这:

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

看起来像是对编译器的另一个名为Asv数组的声明,其运行时大小为i(C99特性)。如果您想分配给索引i处的元素,您可以执行以下操作:

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

请注意,new在堆上进行分配并返回指针,这在这里不是必需的。

您已经声明了一个数组

sv A[100];

要填充数组,只需使用

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

相关内容

最新更新