I'm trying to create an array of my object I made, and basically the object doesn't have a default constructor, only a self made one. I've made my object array in the header file e.g. Object myObject[4];, however doing it this way requires for me to have a default constructor, however I only want to use my constructor. Is there a way around this?


Thanks!

I didn't think of that. Will give it a go. Thanks!

Let me elaborate on my excessively terse answer before.

When you create an array, you have to say how many elements it has, and those elements are all constructed. That means that the type needs a constructor.

When you create a vector, you have two additional options. First, you can create a vector without any elements. If it has no elements, there is no need to construct them. Second, you can create a vector with a given number of elements, all of which are copies of a value that you supply. In neither case do you have to create an element out of nothingness.

So if you can get by with using a vector instead of an array, it is apt to be the easiest way to solve your problem.

Thanks very much for that. Seems to be working ok so far!

To add to what arkoenig mentioned, you can also provide an initialization list to your array. For instance:

struct Foo {
    int x;
    Foo(int i) : x(i) {}
};

// Now you can do
Foo foos[3] = { 1, 2, 3 }; // initializes with Foo(1), Foo(2), Foo(3)

This is limited in that you have to provide a value for each element in the list but for specific instances it can be useful.

In almost all cases a vector outweighs the use of an array.

To add to what arkoenig mentioned, you can also provide an initialization list to your array. For instance:

struct Foo {
    int x;
    Foo(int i) : x(i) {}
};

// Now you can do
Foo foos[3] = { 1, 2, 3 }; // initializes with Foo(1), Foo(2), Foo(3)

This is limited in that you have to provide a value for each element in the list but for specific instances it can be useful.

In almost all cases a vector outweighs the use of an array.

The problem is, values are passed into a function that determines what will be in the array/vector and so I can't do it that way..

Still good to know though, I'm sure it will come in useful at some point. Thanks!

Edited 5 Years Ago by XA04: n/a

Just to be clear, the problem is not that you have made your own constructor. The problem is that it takes one or more arguments. Why not simply add a constructor with no parameters, or add default arguments like this:

#include <iostream>

class MyClass
{
public:
  MyClass(int val = 0);
  virtual ~MyClass(){}
  int x;
};

MyClass::MyClass(int val) : x(val)
{
}

int main()
{
  MyClass vek[10];

  std::cout << vek[3].x << std::endl;
  return 0;
}

I assume you would have initialized each element later on anyway?
Declaring variables in a header file is not recommended, you should put that in a cpp file. What is your reason for doing this?

This article has been dead for over six months. Start a new discussion instead.