简体   繁体   English

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

[英]c++ Reading a file into a vector of objects, then copying to vector pointer

I am writing a program to read from a file and take each line into an object and have all the contents of that line separated into their own variables in that object.我正在编写一个程序来从文件中读取并将每一行放入 object 并将该行的所有内容分隔到 object 中它们自己的变量中。 I got that part down, it's just that the functions that have been defined for me are like so我把那部分记下来了,只是为我定义的功能是这样的

void printSpecs(vector<watercraft *> inventory). 

Can you help me take the input from the file into a vector of objects, then pass that vector into a function like the one above?你能帮我把文件中的输入变成一个对象向量,然后像上面那样将该向量传递给 function 吗?

Here is what I have so far but it is not right.这是我到目前为止所拥有的,但它是不正确的。

    vector <watercraft *> test;
ifstream inFile;
inFile.open("watercraft.txt", ios::in);
if(!inFile){
    cout << "Something wrong with input file" << endl;
    exit(0);
}                    
                for(int i = 0; i < 18; i++){
                    watercraft tempPtr(inFile);
                    test.push_back(&tempPtr);
                }

It results in this结果是这样

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

All the same line which is actually the last line of the file.所有同一行,实际上是文件的最后一行。 Thanks!谢谢!

tempPtr is local to its scope and is invalidated at the end of its scope. tempPtr对其 scope 是本地的,并在其 scope 结束时失效。 Therefore, storing its pointer and dereferencing the pointer later is dangerous.因此,存储它的指针并在以后取消引用指针是危险的。

For example, you can create objects on heap so that they won't be deleted by exiting scope.例如,您可以在堆上创建对象,这样它们就不会通过退出 scope 被删除。

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

Another option is creating a vector of watercraft , then adding pointers to each elements.另一种选择是创建一个watercraft向量,然后添加指向每个元素的指针。 (Don't add elements while creating the vector because re-allocation may invalidate existing pointers): (不要在创建向量时添加元素,因为重新分配可能会使现有指针无效):

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]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM