3

我无法为它造出合适的词来称呼它,所以我就这样取了这个标题。

我有这个 DLL,其中templatedstruct位于 a 中namespace,其中定义了重载运算符struct,这些运算符是这些s的非成员(自由函数) 。

“矢量X.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;
}

然后我尝试使用它

“测试.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;
}

这个有效p += 3 * 2 * 1;,但这个无效p += 3 * 2 * 1 * p1;

我认为这是由于我在内部声明的自由函数的重载运算符namespace导致了错误,但我不知道还能做什么。

4

1 回答 1

3

当你初始化一个被声明的变量时,比如在

Vec3f p = uuu;

您调用复制构造函数。即格式的构造函数

Vec3(const Vec3<T> &v);

通常编译器可以自动生成这样的构造函数,但如果有任何其他非标准构造函数(如那个Vec3(T value);),编译器将不会这样做。这意味着您尝试在没有复制构造函数时调用。

如果您无法控制Vec3该类,因此无法添加复制构造函数,则必须改用赋值:

Vec3f p;
p = uuu;
于 2013-11-07T08:20:06.840 回答