Incomplete type is not valid when trying to create an array of pointers

I created 2 classes, Branch and Account, and I want my Branch class to have an array of account pointers, but I do not. It states that "an incomplete type is not acceptable." 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--------------*/ }; 
+4
source share
2 answers

Although you can create an array of pointers for classes declared ahead, you cannot create an array with an unknown size. If you want to create an array at runtime, make a pointer to a pointer (which, of course, is also allowed):

 Account **ownedAccounts; ... // Later on, in the constructor ownedAccounts = new Account*[numOwnedAccounts]; ... // Later on, in the destructor delete[] ownedAccounts; 
+9
source

You need to specify the size of the array ... You cannot just leave the brackets hanging as if there was nothing inside.

+3
source

All Articles