dimanche 19 septembre 2021

Why template destrcutor cannot be referenced and is deleted

I'm learning C++ and try to use template class to create Vector3. My header file vector.h defining the Vector3 struct:

template <typename T>
struct Vector3 {
  union {
    struct {
      T x, y, z
    } xyz;
    std::vector<T> values;
  };

  Vector3() : values(3, 0) {}
  Vector3(T x, T y, T z) : xyz{x, y, z} {}
};

But when I use this struct with specified type int, I met the error:

function "Vector3::~Vector3() [with T=int]" (declared implicitly) cannot be referenced -- it is a deleted function

I just use it by single line: Vector3<int> vertex;.

Total code: I want to read Wavefront.obj file: model.h:

class ObjModel {
 private:
  std::vector<Vector3<int>> vertices;

 public:
  explicit ObjModel(const std::string& filename);
};

model.cpp:

ObjModel::ObjModel(const std::string& filename) : vertices() {
  std::ifstream in;
  in.open(filename.c_str());
  std::string line;
  while (std::getline(in, line)) {
    if (line.empty() || '#' == line[0]) continue;
    if ('v' == line[0]) {
      std::istringstream iss(line);
      iss.ignore(std::numeric_limits<std::streamsize>::max(), ' ');
      Vector3<int> vertex; **// here has problem**
      int i = 0;
      while (iss && 3 > i) {
        iss >> vertex.values[i];
        ++i;
      }
      vertices.push_back(vertex);
    } 
  }
}

The line Vector3<int> vertex gave the error.

I think the destructor should be default and don't know why it is deleted. I also tried to explicit declare it by adding the following code in vector.h:

struct Vector3{
  ...
  ~Vector3() = default;
  ...
}

But it didn't work. Anyone can help me?

Aucun commentaire:

Enregistrer un commentaire