pointers with bank account

i'm having trouble figuring how to make a pointer to my main function from my account class and my derived checking account class and savings account class.
this is what i got. it doesnt have the savingsccount in there yet. but i'm trying to make it so theres a constructor that has my derived classes of checkingaccount and savingsaccount i get this error
error C3867: 'CheckingAccount::doDebit': non-standard syntax; use '&' to create a pointer to member

1
2
3
4
5
6
  int main()
{
	Account * accounts;
	accounts = new Account[2];
	CheckingAccount c;
	accounts[0] = & c;
Your accounts is a pointer. The pointer points to first element of an array that has two Account objects (line 4).

Your c is a CheckingAccount object. On line 6 you try to take the address of that object and store the address into an object.

Does this look sane?
1
2
3
double foo = 3.14;
int * pbar;
foo = pbar;

It should not.


This would not stop on syntax error:
1
2
3
4
5
int main()
{
  CheckingAccount c;
  SavingAccount s;
  Account* accounts[] = { &c, &s };

Logical sanity ... not guaranteed.


Oh, your error message has nothing to do with the code that you show. That error is elsewhere.
since SavingsAccount and CheckingAccount are derived from Account consider using base class pointers to access derived classes as well - a simple example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# include <iostream>
# include <vector>
# include <memory>

struct Account
{
    virtual void id()const {std::cout << "account \n";}
};
struct CheckingAccount : public Account
{
    void id()const final {std::cout << "checking account \n";}
};
struct SavingsAccount : public Account
{
    void id()const final { std::cout << "savings account \n";}
};

int main()
{
    std::vector<std::unique_ptr<Account>> my_accounts{};

    my_accounts.emplace_back(std::make_unique<Account>());
    my_accounts.emplace_back(std::make_unique<CheckingAccount>());
    my_accounts.emplace_back(std::make_unique<SavingsAccount>());

    for (const auto& elem : my_accounts) elem -> id();
}

Last edited on
Topic archived. No new replies allowed.