vendredi 24 août 2018

Can't use function in inheritance using template

When i use template in inheritance and polymorphism, the compiler gives me an error like this. Error 1:enter image description here .

Error 2:enter image description here

and this is my code

#include <iostream>

template <typename T1, typename T2>
class Parent {
protected:
  T1 _data1;
  T2 _data2;

public:
  Parent(T1 init_data1, T2 init_data2)
    : _data1(init_data1), _data2(init_data2)
  {
    std::cout << "Parent created" << std::endl;
  }

  virtual ~Parent()
  {
    std::cout << "Parent deleted" << std::endl;
  }

  virtual T2 multiple() = 0;
};

template <typename T1, typename T2>
class Child : public Parent<T1, T2> {
public:
  Child(T1 init_data1, T2 init_data2)
    : Parent<T1, T2>(init_data1, init_data2)
  {
    std::cout << "Child created" << std::endl;
  }

  ~Child()
  {
     std::cout << "Child deleted" << std::endl;
  }

  T1 get_data1() const { return this->_data1; }

  T2 get_data2() const { return this->_data2; }

  T2 multiple<T1, T2>() override
  {
    return _data1 * _data2;
  }
};

template <typename T1, typename T2>
std::ostream& operator<<(
  std::ostream& output,
  const Child<T1, T2>* child
)
{
  output
    << child->get_data1() << ", "
    << child->get_data2() << std::endl;
  return output;
}

int main(int argc, char* argv[])
{
  Parent<int, float>* child = new Child(1, 4.5);
  float num = child->multiple();
  std::cout << num << "\n";
  std::cout << child;
  delete child;
}

I don't know why my inheritance didn't work Anyone can solve this problem for me. I appreciate for your support. Thanks a lot

Aucun commentaire:

Enregistrer un commentaire