I created 2 classes, Branch and Account and I want my Branch class have an array of Account pointers, but i fail to do it. It says that "incomplete type is not allowed". What is wrong with my code?
#include <string>
#include "Account.h"
using namespace std;
class Branch{
/*--------------------public variables--------------*/
public:
Branch(int id, string name);
Branch(Branch &br);
~Branch();
Account* ownedAccounts[]; // error at this line
string getName();
int getId();
int numberOfBranches;
/*--------------------public variables--------------*/
/*--------------------private variables--------------*/
private:
int branchId;
string branchName;
/*--------------------private variables--------------*/
};
Although you can create an array of pointers to forward-declared classes, you cannot create an array with an unknown size. If you want to create the array at runtime, make a pointer to a pointer (which is of course also allowed):
Account **ownedAccounts;
...
// Later on, in the constructor
ownedAccounts = new Account*[numOwnedAccounts];
...
// Later on, in the destructor
delete[] ownedAccounts;
You need to specify the size of the array... You can't just leave the brackets hanging like that without anything inside them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With