1

I am trying to create an array of type Vertex and then initialize each member in a function. The Vertex class takes a Vector3f in the constructor:

Vertex::Vertex(const Vector3f& position) : position(position) { }

In the header file I declare the array like this:

class Application
{
    //...
    private:
        Vertex data[3];
    //...
};

and in the source file, in a function I try this:

data[0] = Vertex(Vector3f(0, 0, 0));
data[1] = Vertex(Vector3f(0, 0, 0));
data[2] = Vertex(Vector3f(0, 0, 0));

But when I try to compile I get this error:

/home/mert/dev/C++/C++3D/src/Application.h: In constructor ‘Application::Application()’:
/home/mert/dev/C++/C++3D/src/Application.h:31:19: error: no matching function for call to ‘Vertex::Vertex()’
     Application() { }
                   ^

I have tried declaring the array as a Vertex pointer and then doing data = new Vertex[3]; but the result was the same. What should I do to fix this?

Mertcan Ekiz
  • 691
  • 1
  • 9
  • 22
  • Vertex needs a default constructor to be inside an array like that. – Neil Kirk Oct 15 '14 at 01:10
  • Do you mean a constructor without any arguments? – Mertcan Ekiz Oct 15 '14 at 01:11
  • Yes or your existing constructor with a default parameter. – Neil Kirk Oct 15 '14 at 01:11
  • 2
    Well, you don't need a default constructor if you initialize each element yourself. – chris Oct 15 '14 at 01:12
  • @chris Can that be done with C-style array member? – Neil Kirk Oct 15 '14 at 01:14
  • 2
    @NeilKirk, I believe only in C++11 – chris Oct 15 '14 at 01:15
  • It is possible in C++03, with the help of a helper function (typically a non-static member, but can be free also) IIRC – Ben Voigt Oct 15 '14 at 01:17
  • @BenVoigt, And that works for a bultin array? I could see it working with, e.g., a `boost::array`. – chris Oct 15 '14 at 01:18
  • @chris: You might need [the array placed in a sub-structure](http://stackoverflow.com/a/4058038/103167), to get a copy constructor. But you don't need help from any fancy libraries. – Ben Voigt Oct 15 '14 at 01:28
  • @BenVoigt, Ah, I see. It's the same concept, but with inheritance added as well. – chris Oct 15 '14 at 01:35
  • @chris: Inheritance or member, makes no difference. Any subobject will provide aggregate initialization syntax and automatic generation of a copy constructor. What's tricky is getting an array whose bound is determined by a template parameter, and the element type isn't default constructible. – Ben Voigt Oct 15 '14 at 01:37

1 Answers1

2

Vertex class has no default constructor. When you declare an array of objects, each array entry is built by calling the default constructor. You may fix this either by adding a default construcor or by declaring an array of Vertex pointers and then calling the correct constructor when instantiating the object.

fhsilva
  • 1,217
  • 1
  • 9
  • 17
  • 2
    Instead of the latter it would be better to create `std::vector` and push them back. – Neil Kirk Oct 15 '14 at 01:15
  • @NeilKirk: Certainly not! As there are always exactly 3 elements, that would be badly over-engineered, and the runtime-penalty for that smarts. One could use a `std::array` to avoid raw arrays. Though there's little if any gain. – Deduplicator Oct 15 '14 at 01:16
  • @Deduplicator Compared to "declaring an array of Vector pointers and then calling the correct constructor when instantiating the object" which I assumed is new'ing them. – Neil Kirk Oct 15 '14 at 01:17
  • @NeilKirk: There should not be any `new`-ing going on, and I don't quite see why you would send the OP's broken attempt that way. – Deduplicator Oct 15 '14 at 01:19
  • @Deduplicator No there shouldn't. But if there is, a vector should be used instead. That is what I am saying. – Neil Kirk Oct 15 '14 at 01:20
  • Thanks for the answer fhsilva. And this was a test case so there will be more than 3 Vertices in the finished product (and not fixed numbers as I will be reading from a model file) so I think I will be using a `std::vector` then. But for now adding a default constructor fixed the problem – Mertcan Ekiz Oct 15 '14 at 01:26
  • I agree that, if the requirement is to put N Vertex within the array, a vector might be better. But even so, if there are performance constraints involved, there will be overhead of copying each Vertex object to the vector. – fhsilva Oct 15 '14 at 01:27
  • I will be loading the file only once at the start of my application so the overhead will not be a big problem I think – Mertcan Ekiz Oct 15 '14 at 01:31