mardi 3 décembre 2019

Explicitly calling an underlying template's constructor that holds another template as a data type

I'm working on a custom stack whose underlying container is a custom dynamic vector that holds custom pairs. Here's a snippet from the header:

template <typename T>
class StackMax {
private:
    DV<Couple<T,T> > data;
public:
    StackMax  (unsigned cap = 1);
    // ...
};

My problem comes when calling the stack's constructor to initialise the dynamic vector, which is written like this:

template <typename T>
StackMax<T> :: StackMax (unsigned cap) {
    data = new DV<Couple<T,T> >;
}

I'm trying to create a StackMax<int>, but I get this error:

g++ ./src/test.cpp -o ./bin/test -Iinclude
In file included from ./src/test.cpp:4:
./src/stack_max.cpp: In instantiation of ‘StackMax<T>::StackMax(unsigned int) [with T = int]’:
./src/test.cpp:8:15:   required from here
./src/stack_max.cpp:5:8: error: invalid user-defined conversion from ‘DV<Couple<int, int> >*’ to ‘const DV<Couple<int, int> >&’ [-fpermissive]
    5 |  data = new DV<Couple<T,T> >;
      |  ~~~~~~^~~~~~~~~~~~~~~~~~~~~~
In file included from include/stack_max.hpp:13,
                 from ./src/stack_max.cpp:1,
                 from ./src/test.cpp:4:
include/dv.hpp:16:2: note: candidate is: ‘DV<T>::DV(unsigned int) [with T = Couple<int, int>]’ <near match>
   16 |  DV  (unsigned capacidad = 1);
      |  ^~
include/dv.hpp:16:2: note:   conversion of argument 1 would be ill-formed:
In file included from ./src/test.cpp:4:
./src/stack_max.cpp:5:8: error: invalid conversion from ‘DV<Couple<int, int> >*’ to ‘unsigned int’ [-fpermissive]
    5 |  data = new DV<Couple<T,T> >;
      |  ~~~~~~^~~~~~~~~~~~~~~~~~~~~~
      |        |
      |        DV<Couple<int, int> >*
./src/stack_max.cpp:5:8: error: invalid conversion from ‘DV<Couple<int, int> >*’ to ‘unsigned int’ [-fpermissive]
In file included from include/stack_max.hpp:13,
                 from ./src/stack_max.cpp:1,
                 from ./src/test.cpp:4:
include/dv.hpp:16:16: note:   initializing argument 1 of ‘DV<T>::DV(unsigned int) [with T = Couple<int, int>]’
   16 |  DV  (unsigned capacidad = 1);
      |       ~~~~~~~~~^~~~~~~~~~~~~
include/dv.hpp:5:7: note:   initializing argument 1 of ‘constexpr DV<Couple<int, int> >& DV<Couple<int, int> >::operator=(const DV<Couple<int, int> >&)’
    5 | class DV {
      |       ^~
make: *** [Makefile:9: build] Error 1

The custom pair is just a template class that holds a T1 first and T2 second, just like the official one. The custom vector does have a T* data, which has to be initialised from a constructor, but I've been banging my head against a wall for the last two hours without figuring out what I'm doing wrong.

I'm copiling with C++11 and no, I cannot use the STL, if that was the case we wouldn't be here.

What am I doing wrong?

Aucun commentaire:

Enregistrer un commentaire