Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;将文件读入对象向量,然后复制到向量指针_C++_Vector - Fatal编程技术网

C++ c++;将文件读入对象向量,然后复制到向量指针

C++ c++;将文件读入对象向量,然后复制到向量指针,c++,vector,C++,Vector,我正在编写一个程序,从一个文件中读取,并将每一行放入一个对象中,并将该行的所有内容分离到该对象中各自的变量中。我把那部分记下来了,只是为我定义的函数是这样的 void printSpecs(vector<watercraft *> inventory). 都是同一行,实际上是文件的最后一行。谢谢 temptr是其作用域的局部,在其作用域结束时无效。因此,存储其指针并在以后取消对指针的引用是危险的 例如,您可以在堆上创建对象,以便退出作用域时不会删除它们 for(int i = 0

我正在编写一个程序,从一个文件中读取,并将每一行放入一个对象中,并将该行的所有内容分离到该对象中各自的变量中。我把那部分记下来了,只是为我定义的函数是这样的

void printSpecs(vector<watercraft *> inventory). 

都是同一行,实际上是文件的最后一行。谢谢

temptr
是其作用域的局部,在其作用域结束时无效。因此,存储其指针并在以后取消对指针的引用是危险的

例如,您可以在堆上创建对象,以便退出作用域时不会删除它们

for(int i = 0; i < 18; i++){
    test.push_back(new watercraft(inFile));
}
1: pontoon  Bentley 200 Cruise  Mercury 90  Silver  20  37795
2: pontoon  Bentley 200 Cruise  Mercury 90  Silver  20  37795
3: pontoon  Bentley 200 Cruise  Mercury 90  Silver  20  37795
4: pontoon  Bentley 200 Cruise  Mercury 90  Silver  20  37795
5: pontoon  Bentley 200 Cruise  Mercury 90  Silver  20  37795
6: pontoon  Bentley 200 Cruise  Mercury 90  Silver  20  37795
7: pontoon  Bentley 200 Cruise  Mercury 90  Silver  20  37795
8: pontoon  Bentley 200 Cruise  Mercury 90  Silver  20  37795
9: pontoon  Bentley 200 Cruise  Mercury 90  Silver  20  37795
10: pontoon Bentley 200 Cruise  Mercury 90  Silver  20  37795
11: pontoon Bentley 200 Cruise  Mercury 90  Silver  20  37795
12: pontoon Bentley 200 Cruise  Mercury 90  Silver  20  37795
13: pontoon Bentley 200 Cruise  Mercury 90  Silver  20  37795
14: pontoon Bentley 200 Cruise  Mercury 90  Silver  20  37795
15: pontoon Bentley 200 Cruise  Mercury 90  Silver  20  37795
16: pontoon Bentley 200 Cruise  Mercury 90  Silver  20  37795
17: pontoon Bentley 200 Cruise  Mercury 90  Silver  20  37795
18: pontoon Bentley 200 Cruise  Mercury 90  Silver  20  37795
for(int i = 0; i < 18; i++){
    test.push_back(new watercraft(inFile));
}
vector<watercraft> watercrafts;

for(int i = 0; i < 18; i++){
    watercraft tempPtr(inFile);
    watercrafts.push_back(tempPtr);
}

for (size_t i = 0; i < watercrafts.size(); i++){
    test.push_back(&watercrafts[i]);
}