我定義了一個名爲HPC_user類,如下所示:如何分配類對象的數組
#include <iostream.h>
#include <string>
using std::string;
class HPC_user
{
public:
HPC_user(std::string firstName, std::string lastName, std::string login, std::string school, double activity);
~HPC_user();
std::string get_first() const { return fName; }
void set_first(std::string first) { fName = first; }
std::string get_last() const { return lName; }
void set_last(std::string last) { lName = last; }
std::string get_login() const { return uId; }
void set_login(std::string login) { uId = login; }
std::string get_school() const { return sName; }
void set_school(std::string school) { sName = school; }
std::string get_activity() const {return cpuTime; }
void set_activity(std::string activity) { cpuTime = activity; }
private:
std::string fName, lName, uId, sName, cpuTime;
};
HPC_user.cpp
#include "HPC_user.h"
// constructor of HPC_user
HPC_user::HPC_user(std::string firstName, std::string lastName, std::string login, std::string school, double activity)
{
fName = firstName;
lName = lastName;
uId = login;
sName = school;
cpuTime = activity;
// cout << "new HPC_user created\n";
}
HPC_user::~HPC_user() // destructor
現在我想分配500個HPC_user對象的數組,並設置元件爲NULL或0.0第一。然後在for循環中分配實際值。
這是我做過什麼:
int size = 500;
HPC_user *users;
users = new HPC_user(NULL,NULL,NULL,NULL,0.00)[size];
我在編譯時就發生錯誤:
db2class.cpp:51:49: error: expected ';' after expression
users = new HPC_user(NULL,NULL,NULL,NULL,0.00)[size];
什麼是爲對象數組分配空間的正確方法?
的[動態分配對象的數組(HTTP可能重複:// stackoverflow.com/questions/255612/dynamically-allocating-an-array-of-objects) – drahnr