I am working on an assignment in C++, a language I am not particularly proficient in.
I am attempting to declare a dynamic array of ‘Account’ objects in a file main.cpp:
Account * acctArray = new Account[];
main.cpp includes Account.h:
class Account {
private:
int customerID;
int BSB;
int acctNumber;
string surname;
string firstName;
double balance;
double withdrawn;
public:
Account() {};
//setters
void setCustID(int ID);
void setBSB(int inBSB);
void setAcctNo(int number);
void setSurname(string sname);
void setFirstName(string fname);
void setBalance(double bal);
void setWithdrawn(double withd);
//getters
//(snipped for irrelevance)
//methods
bool withdraw(double amount);
};
However, when compiling on my uni’s unix machine (the machine the assignment must be submitted on), I get the following error:
“main.cpp”, line 130: Error: The type “Account[]” is incomplete.
I tried compiling with
Account * acctArray = new Account[5];
to see if I could isolate the problem, and this line compiled fine.
What am I doing wrong?? I fear the solution lies in pointers/references and my lack of understanding thereof.
An array in C++ has a fixed size. There is no built-in “dynamic array” functionality. If you want a dynamic array, use a
std::vector<Account>.It is best to avoid
newand explicit dynamic allocation wherever possible. If you think you need to dynamically allocate something usingnewand manage it yourself, there’s probably a better way to accomplish the task.