Bur*_*men 5 c++ arrays include
我创建了2个类,分支和帐户,我希望我的Branch类有一个Account指针数组,但我没有做到.它说"不允许不完整的类型".我的代码出了什么问题?
#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--------------*/
};
Run Code Online (Sandbox Code Playgroud)
das*_*ght 11
虽然您可以创建指向前向声明的类的指针数组,但您无法创建具有未知大小的数组.如果要在运行时创建数组,请创建指针指针(当然也允许):
Account **ownedAccounts;
...
// Later on, in the constructor
ownedAccounts = new Account*[numOwnedAccounts];
...
// Later on, in the destructor
delete[] ownedAccounts;
Run Code Online (Sandbox Code Playgroud)