我有一个模板矢量类,可以在其中更改坐标的类型 我已经将声明和定义分别放在.h和.cpp文件中。这是代码:
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
注意:我跳过了引用外部的符号和函数,因为它们并不重要
我正在使用Visual Studio 2017,我的计算机最近在打开项目时因电池电量不足而关闭了,这是造成问题的原因吗?
我现在真的迷失了,我有了定义,所以我不明白为什么编译器会生我的气...
在此先感谢您的帮助:)