0

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());
};

And here are the errors: build output

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 :)

Angramme
  • 164
  • 1
  • 10
  • _"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?"_ Nope. – πάντα ῥεῖ Jun 08 '19 at 12:01
  • I swear I looked around for 1h and didn't find this answer... But it's solved so that doesn't matter I guess (apart from -2 :( for a duplicate) – Angramme Jun 08 '19 at 12:02
  • _"I swear I looked around for 1h and didn't find this answer... "_ Comes up pretty quickly with a simple google search about _c++ templates undefined reference_ : https://www.google.com/search?q=c%2B%2B+template+undefined+reference+site:stackoverflow.com&rlz=1C1CHBF_deDE833DE833&sa=X&ved=2ahUKEwjf3YzU69niAhUIYlAKHVhlAEkQrQIoBDAAegQIBhAM&biw=1600&bih=789 (narrowed results by site stackoverflow). Maybe you've been searching based on some wrong assumptions, or used the wrong keywords to search. – πάντα ῥεῖ Jun 08 '19 at 12:05
  • BTW: The downvote here was probably because you have an image for the errors rather than the errors as text. While the image might be useful in addition to text, text should be the primary way to include such things in your posts. The following comment is a stock comment I often leave for people who use images instead of text. – Makyen Jun 19 '19 at 18:41
  • Please add code, errors and data as **text** ([using code formatting](//stackoverflow.com/editing-help#code)), not images. Images: A) don't allow us to copy-&-paste the code/errors/data for testing; B) don't permit searching based on the code/error/data contents; and [many more reasons](//meta.stackoverflow.com/a/285557). In general, code/errors/data in text format >>>> code/errors/data as an image >> nothing. Images should only be used, *in addition to text in code format*, if having the image adds something significant that is not conveyed by just the text code/error/data. – Makyen Jun 19 '19 at 18:42
  • @Makyen did it, thanks for the suggestion – Angramme Jun 19 '19 at 21:04

0 Answers0