I have a template vector class where I can change the type of the coordinates I've put declaration and definition separetly both in .h and .cpp files. Here is the code:
Vector.h:
#pragma once
#include <iostream>
template <class T>
struct Vec3 {
union {
struct {
T r, g, b;
};
struct {
T x, y, z;
};
T data[3];
};
Vec3()
: x(0), y(0), z(0)
{};
Vec3(int a, int b, int c)
: x(a), y(b), z(c)
{};
Vec3<T>& operator+=(const Vec3<T>& o);
Vec3<T>& operator-=(const Vec3<T>& o);
Vec3<T>& operator*=(const Vec3<T>& o);
Vec3<T>& operator/=(const Vec3<T>& o);
Vec3<T> operator+(const Vec3<T>& o) const;
Vec3<T> operator-(const Vec3<T>& o) const;
Vec3<T> operator*(const Vec3<T>& o) const;
Vec3<T> operator/(const Vec3<T>& o) const;
T dot(const Vec3<T>& o) const;
T length() const;
T lengthSQ() const;
Vec3<T> normalize();
};
template <class T>
std::ostream & operator << (std::ostream &in, Vec3<T>&v)
{
std::cout << "Vec3<"
<< typeid(T).name()
<< "> {" << v.x << ", " << v.y << ", " << v.z << "};";
return in;
}
using Vec3f = Vec3<float>;
using Vec3i = Vec3<int>;
Vector.cpp:
#pragma once
#include "Vector.h"
#include <cmath>
template <class T>
Vec3<T>& Vec3<T>::operator+=(const Vec3<T>& o) {
this->x += o.x;
this->y += o.y;
this->z += o.z;
};
template <class T>
Vec3<T>& Vec3<T>::operator-=(const Vec3<T>& o) {
this->x -= o.x;
this->y -= o.y;
this->z -= o.z;
};
template <class T>
Vec3<T>& Vec3<T>::operator*=(const Vec3<T>& o) {
this->x *= o.x;
this->y *= o.y;
this->z *= o.z;
};
template <class T>
Vec3<T>& Vec3<T>::operator/=(const Vec3<T>& o) {
this->x /= o.x;
this->y /= o.y;
this->z /= o.z;
};
template <class T>
Vec3<T> Vec3<T>::operator+(const Vec3<T>& o) const {
return Vec3<T>(
this->x + o.x,
this->y + o.y,
this->z + o.z
);
};
template <class T>
Vec3<T> Vec3<T>::operator-(const Vec3<T>& o) const {
return Vec3<T>(
this->x - o.x,
this->y - o.y,
this->z - o.z
);
};
template <class T>
Vec3<T> Vec3<T>::operator*(const Vec3<T>& o) const {
return Vec3<T>(
this->x * o.x,
this->y * o.y,
this->z * o.z
);
};
template <class T>
Vec3<T> Vec3<T>::operator/(const Vec3<T>& o) const {
return Vec3<T>(
this->x / o.x,
this->y / o.y,
this->z / o.z
);
};
template<class T>
T Vec3<T>::dot(const Vec3<T>& o) const {
return this->x * o.x + this->y * o.y + this->z * o.z;
};
template <class T>
T Vec3<T>::lengthSQ() const {
return this->x*this->x + this->y*this->y + this->z*this->z;
};
template<class T>
T Vec3<T>::length() const {
return std::sqrtf(this->lengthSQ());
};
template <class T>
Vec3<T> Vec3<T>::normalize() {
return (*this)*(1 / this->length());
};
LNK2019 : unresolved external symbol "public:struct Vec3<float> _thiscall Vec3<float>::operator+(struct Vec3<float> const&)const" (object symbol...) referenced in function "some function" (some different symbol...)
LNK2019 : unresolved external symbol "public:struct Vec3<float> _thiscall Vec3<float>::operator-(struct Vec3<float> const&)const" (object symbol...) referenced in function "some function" (some different symbol...)
(...)
LNK1120 : 4 unresolved externals
NOTE: I skipped symbols and functions which referenced the externals as they are not important
I'm using visual studio 2017, my computer recently closed itself when low on battery while my project was opened, meaby that's the cause?
I really am lost now, I have definitions so I don't see why the compiler would get angry at me...
Thanks in advance for your help :)