1

I'm creating a simple vector class in C++ using dynamic arrays. The constructor with an int parameter must be deleted.

I have two errors:

  • Deleted definition of simple_vector::simple_vector(int) from my cpp file.

  • Previous declaration of simple_vector::simple_vector(int) from my hpp file.

What's going on? The name of my class is simple_vector. It can only contain double elements.

Honestly, I have no idea what to do.

Hpp file

simple_vector(const int a);

Cpp file

simple_vector::simple_vector(const int a) = delete;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Make your constructor explicit. https://stackoverflow.com/questions/121162/what-does-the-explicit-keyword-mean – Retired Ninja Mar 25 '19 at 00:29
  • 2
    @RetiredNinja You get better error messages if deleted functions are not explicit ("No viable function" becomes "Use of deleted function" when trying to implicitly convert, which makes the intent that you can't construct from `int` more clear) – Artyer Mar 25 '19 at 00:33
  • 1
    Unless you plan to write a deleted constructor for every possible type that could convert then explicit is still a good idea if you do not want implicit conversion. It doesn't have to be a one or the other thing. – Retired Ninja Mar 25 '19 at 00:36
  • As a point of information, C++ does not support dynamic arrays. – Michael Surette Mar 25 '19 at 00:53

1 Answers1

2

You can't mark your constructor as delete'd on its definition in the cpp file, you need to mark it on its declaration in the hpp file instead.

In the hpp file, change this:

simple_vector(const int a);

To this:

simple_vector(const int) = delete;

And in the cpp file, remove this completely:

simple_vector::simple_vector(const int a) = delete;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770