Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined reference: dll -> free function -> overloaded operator -> templated struct

I can't build the right words for it to call, so I made the title like that.

I have this DLL in which the templated structs are inside in a namespace with defined overloaded operators that are non-member(free function) of those structs.

"VectorX.h"

#ifdef SPECS
#define SPECS __declspec(dllimport)
#else
#define SPECS __declspec(dllexport)
#endif // SPECS

namespace swc
{
    template <typename T>
    struct Vec3 : Vec2<T>
    {
        Vec3();
        Vec3(T value); //*1
        Vec3& operator = (const T scalar); //*1
        ...
        Vec3& operator += (const Vec3<T> &v);
        Vec3& operator += (const T scalar);

        //*1 w/c of this two are used when I do `Vec3<T> v = 0.0f;` ??
    };

    //What attribute should I use to make this works?
    //It's compiling but it cause undefined reference when I use it.
    //Or I have many ambiguous calls here(?)

    template<typename T>
    /* SPECS, static, extern */ Vec3<T> const operator * (const Vec3<T> &v, const T scalar);
    template<typename T>
    /* SPECS, static, extern */  Vec3<T> const operator * (const T scalar, const Vec3<T> &v);

    typedef Vec3<float> Vec3f;
}

then I tried to use it

"test.cpp"

#include <iostream>

#include "../../VectorX/vectorx.h"

using namespace swc;

Vec3f CalculateBezierPoint(float t, const Vec3f &p0, const Vec3f &p1, const Vec3f &p2, const Vec3f &p3);

int main()
{
    ...
}

Vec3f CalculateBezierPoint(float t, const Vec3f &p0, const Vec3f &p1, const Vec3f &p2, const Vec3f &p3)
{
    float u = 1 - t;
    float tt = t * t;
    float uu = u * u;
    float uuu = uu * u;
    float ttt = tt * t;

    Vec3f p = uuu; //is it the operator '=' or the constructor Vec3f(T value)?
    //**this is where the compiler starts complaining about undefined references.**
    p += 3 * uu * t * p1;
    p += 3 * u * tt * p2;
    p += ttt * p3;

    return p0;
}

this one works p += 3 * 2 * 1; but this one doesn't p += 3 * 2 * 1 * p1;

I think it is due to the declaration of the overloaded operator of free function inside the namespace I made that causes the error but I don't know what else to do.

like image 762
mr5 Avatar asked Jan 18 '26 00:01

mr5


1 Answers

When you initialize a variable being declared, like in

Vec3f p = uuu;

you invoke the copy constructor. That is, a constructor of the format

Vec3(const Vec3<T> &v);

Normally the compiler can auto-generate such a constructor, but if there are any other non-standard constructor (like the Vec3(T value); one) the compiler will not do that. That means that you try to invoke the copy-constructor when there is none.

If you don't have control of the Vec3 class, and so can't add the copy-constructor, you have to use the assignment instead:

Vec3f p;
p = uuu;
like image 166
Some programmer dude Avatar answered Jan 19 '26 16:01

Some programmer dude



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!