Using an object pointer to access its member functions inside another class

0

I have an account class that has the data members accountNumber, accountBalance, and a pointer to a Person object, accountPerson. All of these are protected because Account is an abstract base class. My program reads info from a file and I am trying to call each objects own readData() methods. For some reason, It is throwing an exception when I call accountPerson->readData(file);.

I am including just the readData functions from the class .cpp files. Let me know if I need to include anything else.

Please take a look at my code and let me know if I'm missing something.

Account.cpp

void Account::readData(ifstream &ifile)
{
    string a;
    string b;

    if (ifile.fail() || ifile.bad())
        {
            throw readFileException("Could not read file");
        }
        else
        {
            getline(ifile, a);
            accountNumber = stoi(a);
            getline(ifile, b);
            accountBalance = stod(b);
            accountPerson->readData(ifile);
        }
}

Person.cpp

void Person::readData(ifstream &ifile)
{
    if (ifile.fail() || ifile.bad())
    {
        throw readFileException("Could not read file");
    }
    else
    {
        getline(ifile, name);
        getline(ifile, address);
    }
}

Savings Account .cpp

void Savings::readData(ifstream &ifile)
{
    string _a;
    if (ifile.fail() || ifile.bad())
    {
        throw readFileException("Could not read file");
    }
    else
    {
        ifile >> interest;
        ifile.ignore();
        Account::readData(ifile);
    }
}

driver.cpp

while (inFile.good())
{
    if (!inFile.eof()) //Loop to read in accounts and push them to vector
    {
        while (inFile >> type)
        {
            if (type == "Checking")
            {
                cout << type << endl;
                a[index] = new Checking();
            }
            else
            {
                cout << type << endl;
                a[index] = new Savings();
            }
            cout << index << endl;
            try
            {
                a[index]->readData(inFile);
            }
            catch (readFileException &e)
            {
                cout << "Error: " << e.getMessage() << endl;
                system("pause");
                exit(1);
            }
            index++;
        }
    }
} // end of while loop

The exception says:

Exception thrown at 0x0016733B in Project3.exe: 0xC0000005: Access violation reading location 0x00000014.

Any tips for a beginner would be much appreciated. Thank you.

c++
pointers
polymorphism
abstract-class
asked on Stack Overflow Oct 26, 2015 by Jason Carrick • edited Oct 26, 2015 by Jason Carrick

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0