【发布时间】:2013-03-27 07:21:29
【问题描述】:
我创建了 2 个类,Branch 和 Account,我希望我的 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--------------*/
};
【问题讨论】:
-
在编译时数组的大小是否已知?另外,您确定需要
Account指针,而不仅仅是对象吗? -
那行没有错误,使用 g++。演示:ideone.com/wczznf 因为我没有
"Account.h",所以我写了:class Account; -
数组大小一开始是0,我会动态扩展。是的,我需要 Account 指针,因为 Account 对象的数组在另一个文件中,我还需要从另一个名为 Customer 的类中指向它。
-
我不明白你的意思。首先,您可以使用
std::vector来调整大小和一切。关于你的结构,如果Customer.h和Branch.h都包含Account.h,那么它们都可以正常使用普通对象。不需要指针。 -
我们不允许使用
std::vector。至于我的结构,我脑子里有点复杂,我也无法解释。