'Why is a default constructor needed here?
I have created a class called Camera that doesn't have a default constructor because I will probably never have to use one. The compiler gives me back an error when I am trying to compile the project:
C2512: 'Vector3< double >': no appropriate default constructor available
The error is raised at the line of the constructor of the Camera class. Here is my 'camera.h' file:
#pragma once
#include "algebra.h"
template <typename T>
class Camera {
public:
Camera(Vector3<T> position, Vector3<T> direction)
: m_position(position), m_direction(direction) {
m_positionNormalized = position.normalize();
m_directionNormalized = direction.normalize();
}
private:
Vector3<T> m_position;
Vector3<T> m_direction;
Vector3<T> m_positionNormalized;
Vector3<T> m_directionNormalized;
};
Here is the 'algebra.h' file defining the Vector3 class:
#pragma once
#include <math.h>
#include <iostream>
template <typename T>
class Vector3 {
public:
Vector3(T x, T y, T z) : m_x(x), m_y(y), m_z(z) {}
T norm() const {
return sqrt(m_x * m_x + m_y * m_y + m_z * m_z);
}
Vector3<T> normalize() {
T m_norm = norm();
try {
if (m_norm == 0.)
throw std::string("Normalization of a zero vector !");
else {
m_x /= m_norm;
m_y /= m_norm;
m_z /= m_norm;
}
}
catch (std::string const& error) {
std::cerr << error << std::endl;
}
return *this;
}
private:
T m_x;
T m_y;
T m_z;
};
Here is the 'main.cpp' where the constructors are called:
#include "camera.h"
#include "algebra.h"
int main(int argc, char *argv[]) {
Vector3<double> position(0., 0., 0.);
Vector3<double> direction(0., 0., 1.);
Camera<double> camera(position, direction);
return 0;
}
I agree that there is no default constructor for the Vector3 class, but I don't understand why it is a problem here, since I don't feel like I am calling it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
