In my C++ project, there is a class which needs to create an array of objects. Between different instances of the class, the size of the array will be different, which is why I chose to use an array.
If I do :
int numberOfPlayers; // This is determined at run time.
int *players;
//In constructor
players= new int[numberOfPlayers]; // This works
But if I do:
Character *players;
players = new Character[numberOfPlayers]; // Compiler complains
The Compiler complains "No matching constructor for initialisation of Character"
How do I dynamically declare an array of type "Character".
Note: Character has nothing to do with char. Character is a name of an class I created myself.
EDIT: Character does not have a default constructor, since it needs to be passed several arguments so it can be initialised with the proper state. The only constructor is has takes several arguments.
EDIT: I chose a dynamically created array, over a vector since I know during the lifetime of the instance, the size of the array will be constant, though between different instances the size will be different. I thought this would make sense for performance reasons (memory / speed).
We can create an array of pointers also dynamically using a double pointer. Once we have an array pointers allocated dynamically, we can dynamically allocate memory and for every row like method 2.
A simple dynamic array can be constructed by allocating an array of fixed-size, typically larger than the number of elements immediately required.
To make a dynamic array that stores any values, we can turn the class into a template: template <class T> class Dynarray { private: T *pa; int length; int nextIndex; public: Dynarray(); ~Dynarray(); T& operator[](int index); void add(int val); int size(); };
The "proper" way is to use std::vector
. It is a fast, safe, more robust alternative to horrible new
.
std::vector<Character> vec;
vec.push_back(Character(params));
vec.push_back(Character(other_params));
If you know the size ahead, you can avoid reallocation overhead by using std::vector::reserve
std::vector<Character> vec;
vec.reserve(50);
vec.push_back(Character(params));
vec.push_back(Character(other_params));
The overhead of std::vector
is practically non-existent.
Now, the reason why you can't do this your way, it's because by default new
uses default constructor, and it doesn't exist.
The problem is that your type Character
does not define a default constructor of the form:
Character::Character()
{
// etc.
}
Your type needs a default constructor. Unlike C's malloc
, operator new
constructs instances for you at the time of allocation. It then follows that it requires a parameterless (default) constructor as it provides no way to pass arguments. So...
class Character
{
public:
Character(){}
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With