指针数组使用情况

时间:2016-05-04 11:15:52

标签: c++

我有一个Movie类,其构造函数需要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

如何在该数组中创建一个电影对象?

1 个答案:

答案 0 :(得分: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])));
相关问题