Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

incomplete type is not allowed while trying to create an array of pointers

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--------------*/
    };
like image 455
Burak Özmen Avatar asked Apr 05 '13 01:04

Burak Özmen


2 Answers

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;
like image 173
Sergey Kalinichenko Avatar answered Oct 14 '22 11:10

Sergey Kalinichenko


You need to specify the size of the array... You can't just leave the brackets hanging like that without anything inside them.

like image 25
2to1mux Avatar answered Oct 14 '22 11:10

2to1mux