使用模板无法在继承中使用函数

时间:2018-08-24 08:59:08

标签: c++ c++11

当我在继承和多态中使用模板时,编译器给我一个错误 像这样
错误1: enter image description here 错误2: enter image description here

这是我的代码

#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;
}

我不知道为什么我的继承无效 任何人都可以为我解决这个问题。感谢您的支持。非常感谢

1 个答案:

答案 0 :(得分:2)

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() override
    {
        //return Parent<T1, T2>::_data1 * Parent<T1, T2>::_data2; // Another option
        return this->_data1 * this->_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<int, float>(1, 4.5);
    float num = child->multiple();
    std::cout << num << "\n";
    std::cout << (Child<int, float>*)child << std::endl;
    delete child;
}

下一行要注意的一点:

std::cout << (Child<int, float>*)child << std::endl;

在此运算符重载中,您使用的是Child类函数,并且此指针是Parent类型的指针,它不包括那些函数(即使不是在虚拟函数中)。如果尝试以自己的方式使用这些功能,则不会获得预期的结果。向我展示的方法是解决此问题的一种方法,另一种方法是在Parent类中创建这些函数,并也将此操作符重载到Parent类中:

template ...
class Parent {
    ...
public:
    ...
    virtual T2 multiple()         = 0;
    virtual T1 get_data1() const  = 0;
    virtual T2 get_data2() const  = 0;

};

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

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