c++ - Array of different objects -
alright trying this.
i have account class, have checkingaccount , savingsaccount class inherit account class.
in program have array called accounts. going hold objects of both types of accounts.
account** accounts; accounts = new account*[numaccounts]; accountfile >> tempaccountnum >> tempbalance >> temptransfee; checkingaccount tempaccount(tempbalance, tempaccountnum, temptransfee); accounts[i] = tempaccount;
i error when trying assign tempaccount accounts array.
no suitable conversion function "checkingaccount" "account" exists.
how make accounts array hold both kinds of objects?
each element in accounts
account*
. is, "pointer account
". you're trying assign account
directly. instead, should taking address of account:
accounts[i] = &tempaccount;
remember pointer pointing @ invalid object once tempaccount
has gone out of scope.
consider avoiding arrays , pointers. unless have reason not to, use std::vector
of account
s (not account*
s):
std::vector<account> accounts; accountfile >> tempaccountnum >> tempbalance >> temptransfee; accounts.emplace_back(tempbalance, tempaccountnum, temptransfee);
Comments
Post a Comment