模板类转换运算符

时间:2019-10-08 18:11:06

标签: c++ templates type-conversion

您好,我目前正在创建模板类,但是此模板的两个类之间的转换运算符存在问题。

class.h:

#ifndef PVECTOR_H
#define PVECTOR_H

#include <initializer_list>

template <class T>
/**
 * @brief Vector class
 */
class PVector {
public:
  PVector(T *data, int size);
  PVector(std::initializer_list<T> data);
  ~PVector();

  /**
   * @brief Return datas ptr.
   * @return
   */
  T *Datas() const;

  /**
   * @brief Return data at given position.
   * @param pos
   * @return
   */
  T Data(int pos) const;

  /**
   * @brief Return vector size.
   * @return
   */
  int Size() const;

  /**
   * @brief operator +
   * @param vec
   * @return
   */
  PVector<T> operator+(const PVector<T> &vec) const;

  /**
   * @brief operator *
   * @param a
   * @return
   */
  PVector<T> operator*(const int &a) const;
  PVector<T> operator*(const float &a) const;
  PVector<T> operator*(const double &a) const;

  template <class U> operator PVector<U>(); //Conversion operator I'm having problem with

private:
  T *_data;
  int _size;
};

#endif // PVECTOR_H

class.cpp :(转换运算符是最后定义的方法)

#include "pvector.h"
#include "perror.h"

#include <iostream>

template <class T>
PVector<T>::PVector(T *data, int size) : _data(data), _size(size) {}

template <class T>
PVector<T>::PVector(std::initializer_list<T> data) : _size(int(data.size())) {
  _data = new T[_size];

  for (int i = 0; i < int(data.size()); ++i) {
    _data[i] = *(data.begin() + i);
  }
}

template <class T> PVector<T>::~PVector() { delete[] _data; }

template <class T> T *PVector<T>::Datas() const { return _data; }

template <class T> T PVector<T>::Data(int pos) const { return _data[pos]; }

template <class T> int PVector<T>::Size() const { return _size; }

template <class T>
PVector<T> PVector<T>::operator+(const PVector<T> &vec) const {
  try {

    if (_size != vec.Size()) {
      throw PVectorSizeError();
    } else {
      T *data = new T[_size];

      for (int i = 0; i < _size; ++i) {
        data[i] = _data[i] + vec.Datas()[i];
      }

      return PVector<T>(data, _size);
    }

  } catch (PVectorSizeError e) {
    std::cout << e.What() << std::endl;
  }
}

/***************************
 * Mult operator
 * ************************/

template <class T> PVector<T> PVector<T>::operator*(const int &a) const {
  return *this * double(a);
}

template <class T> PVector<T> PVector<T>::operator*(const float &a) const {
  return *this * double(a);
}

template <class T> PVector<T> PVector<T>::operator*(const double &a) const {
  T *temp = new T[_size];

  for (int i = 0; i < _size; ++i) {
    temp[i] = _data[i] * a;
  }

  return PVector<T>(temp, _size);
}

/*************************
 * Conversion operator
 * *********************/

template <class T> template <class U> PVector<T>::operator PVector<U>() {
  U *temp = new U[_size];

  for (int i = 0; i < _size; ++i) {
    temp[i] = T(_data[i]);
  }

  return PVector<U>(temp, _size);
}

template class PVector<short int>;
template class PVector<int>;
template class PVector<long int>;
template class PVector<long long int>;
template class PVector<float>;
template class PVector<double>;

我希望转换运算符做什么:

PVector<int> vec1 = {1, 2, 3}

PVector<double> vec2 = PVector<double>(vec1);

错误:error: undefined reference to `_ZN7PVectorIiEcvS_IT_EIdEEv'

我尝试了不同的方法,但是编译器不允许。

已解决: 答案与许多模板错误相同,需要将定义移至头文件。

0 个答案:

没有答案