2016-05-04 81 views
-1

我有一个电影类,其中有构造函数需要7个参数像这样;指针阵列的用法

Movie:: Movie(string ttle,string sts ,double prc,int yr,string gnr,Date rls,int id) 

我想使用动态内存的电影数组,但它给了错误,我无法找到它

int main() { 


    int counter=0; // size of array 
    Movie *moviearray; 
    moviearray= new Movie[counter];  

ifstream filein("DVD_list.txt"); 


    for (string line; getline(filein, line);) 
    { 

     counter++; 

     vector<string> v; 


     split(line, '\t', v); // v is an vector and puts string words that has splitted based on tab 

moviearray[counter] =(v[0],v[1] ,stod(v[2]),stoi(v[3]),v[4],Date(v[5]),stoi(v[6])); // ERROR 

如何创建数组中的电影对象?

+2

您正试图使大小为0的数组,因为'计数器= 0' – CoryKramer

+0

我想数组大小和行在txt文件中是相等的 – pflove

回答

4

此:

int counter=0; // size of array 
moviearray= new Movie[counter];  

没有道理。您正在分配一个零对象数组。稍后你使用它。这是非法的。

相反,尝试:

std::vector<Movie> movies; 

然后在您的循环:

movies.push_back(Movie(v[0],v[1] ,stod(v[2]),stoi(v[3]),v[4],v[5],stoi(v[6])));