如何访问超出其块范围的对象?

时间:2018-10-26 13:51:46

标签: c++ object

嘿,我试图弄清楚如何访问超出块范围的对象。我在For循环中定义了Person personData ,它将数据写入文件。在循环之后,我想再次访问该对象以更新文件上的值,但是它给了我错误- personData 是未定义的标识符。

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cstdlib>
#include "Person.h"

using namespace std;

int main() {

    string lastName;
    string firstName;
    int age;

    //file output and creation
    ofstream outPerson("nameage.dat", ios::out | ios::binary);

    Person randPerson;

    randPerson.setLastName("unassigned");
    randPerson.setFirstName("");
    randPerson.setAge(0);
    randPerson.setId(0);

    //for loop to initialize the file with 100 records that store values lastName and firstName
    for (int i = 0; i < 100; i++) {
        outPerson.write(reinterpret_cast<const char*>(&randPerson), sizeof(Person));  //use write to output to file 
    }

    cout << "File Created" << endl;

    //file input and termination 
    ifstream inPerson("nameage.dat", ios::in | ios::out | ios::binary);

    //loops through 10 times to input 10 values (RECORD VALUES) 
    for (int j = 0; j < 2; j++) {
        int id = 0;
        do {
            cout << "Enter a valid id number: (1-100)" << endl;
            cin >> id;
        } while ((id<1)||(id>100)); //breaks do-while after it receives a valid input 


        Person personData;

        inPerson.seekg((id - 1) * sizeof(Person));
        inPerson.read(reinterpret_cast<char*>(&personData), sizeof(Person));

        //checks to see if there is already data in that area, if not then proceed to record data onto file 
        if (personData.getId() == 0) {
            cout << "Enter lastname: ";
            cin >> lastName;
            cout << "Enter firstname: ";
            cin >> firstName;
            cout << "Enter age: ";
            cin >> age;

            //sets data for the particular object 
            personData.setLastName(lastName);
            personData.setFirstName(firstName);
            personData.setAge(age);
            personData.setId(id);

            //seek position in file of user-specified record 
            outPerson.seekp((personData.getId() - 1) * sizeof(Person));

            //write user-specified information in file 
            outPerson.write(reinterpret_cast<const char*>(&personData), sizeof(Person));

            cout << "Record inserted" << endl;
        }
        else {
            cout << "There is already data there. Try another ID number" << endl;
        }//end if
    }//end for loop


    int idSearch;

    do {
        cout << "Enter a ID number: " << endl;
        cin >> idSearch;
    } while ((idSearch < 1) || (idSearch > 100));

    if (personData.getId() != 0) {
        cout << "Enter new Last name";
        cin >> lastName;
        cout << "Enter new first name";
        cin >> firstName;
        cout << "Enter age";
        cin >> age;

        //sets data for the particular object 
        personData.setLastName(lastName);
        personData.setFirstName(firstName);
        personData.setAge(age);
        personData.setId(idSearch);

        //seek position in file of user-specified record 
        outPerson.seekp((personData.getId() - 1) * sizeof(Person));

        //write user-specified information in file 
        outPerson.write(reinterpret_cast<const char*>(&personData), sizeof(Person));

        cout << "Record updated" << endl;
    }

    inPerson.read(reinterpret_cast<char*>(&personData), sizeof(Person));





    system("pause");
    return 0;
}

我假设问题是因为超出范围时无法访问该对象。因此,我将如何从for循环下面的语句访问对象。谢谢。

1 个答案:

答案 0 :(得分:2)

这是C ++数据模型的核心思想之一:数据在离开范围后即被删除。

要执行此操作,您需要更改personData的范围(例如,将变量定义移到循环外)。

但是要谨慎使用类似的东西。在最佳情况下,personData将存储循环的最后一次迭代剩余的数据。