0

I defined a class called HPC_user as follows:

#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 

Now I want to allocate an array of 500 HPC_user objects and set the elements to NULL or 0.0 first. Then assign real values in a for loop.

This is what I did:

  int size = 500;
  HPC_user *users;
  users = new HPC_user(NULL,NULL,NULL,NULL,0.00)[size];

I got an error while compiling it:

db2class.cpp:51:49: error: expected ';' after expression
users = new HPC_user(NULL,NULL,NULL,NULL,0.00)[size];

What is the correct way to allocate space for an array of objects?

ddd
  • 4,665
  • 14
  • 69
  • 125
  • possible duplicate of [Dynamically allocating an array of objects](http://stackoverflow.com/questions/255612/dynamically-allocating-an-array-of-objects) – drahnr Feb 27 '14 at 15:57

2 Answers2

1

If you think your HPC_user has a reasonable default, add a default constructor to this class:

HPC_user::HPC_user()
   : cpuTime( 0.0 )
{
}

Then you can construct a vector of 500 HPC_user:

std::vector< HPC_user > users( 500 );

And you should use the initialization syntax, when you initialize data, no assigment:

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 )
{
}
Torsten Robitzki
  • 3,041
  • 1
  • 21
  • 35
  • Can I do this in my HPC_user class declare? HPC_user(std::string firstName="", std::string lastName="", std::string login="", std::string school="", double activity=0.0); – ddd Feb 27 '14 at 17:17
  • Yes. you can, but you really shouldn't this, because this causes a lot of overhead on the calling side. I've missed that the cpuTime is a double, in this case it must be initialized. For the string, they have reasonable default constructors. – Torsten Robitzki Feb 27 '14 at 17:20
1

Use an std::vector:

std::vector<HPC_user> users(size, HPC_user(NULL,NULL,NULL,NULL,0.00));

But this will immediately crash because initializing an std::string from a null pointer is an error. So you need to fix the constructor arguments to be something sensible, or provide a sensible default constructor

 HPC_user() : activity(0.0) {} // strings get default constructed to ""

and do

std::vector<HPC_user> users(size);
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • Thanks, what is the advantage of having it as a vector as opposed to an array here? – ddd Feb 27 '14 at 16:38
  • 1
    Safer, easier to write and read and maintain, shorter code, exception safe, value semantics, possibly faster, can grow and shrink if required... std::vector is the default collection mechanism in C++. – Christian Hackl Feb 27 '14 at 16:40
  • @MrWhite Using an array only has disadvantages. I would only use it to implement something like a vector, or a C++14 `std::dynarray`. – juanchopanza Feb 27 '14 at 16:45
  • Instead of initializer list in the class header, can I initialize the member variables directly during the declaration? private: std::string fName = ""; double activity = 0.0; – ddd Feb 27 '14 at 17:41
  • @MrWhite Yes, that is correct, if you have a C++11 compiler. It wouldn't work in C++03. – juanchopanza Feb 27 '14 at 17:48