0

通常当我收到这个错误时,我会去寻找一个我忘记定义的函数,但我是从一个类的赋值运算符那里得到的,我从来没有声明过它。错误是这样的:

1>SYNC_D3D11Model_Defs.obj : error LNK2019: unresolved external symbol "public: struct SYNC::Vector2 & __thiscall SYNC::Vector2::operator=(struct SYNC::Vector2 const &)" (??4Vector2@SYNC@@QAEAAU01@ABU01@@Z) referenced in function "public: struct VERTEX_TYPE & __thiscall VERTEX_TYPE::operator=(struct VERTEX_TYPE const &)" (??4VERTEX_TYPE@@QAEAAU0@ABU0@@Z)

1>SYNC_D3D11Model_Defs.obj : error LNK2019: unresolved external symbol "public: struct SYNC::Vector4 & __thiscall SYNC::Vector4::operator=(struct SYNC::Vector4 const &)" (??4Vector4@SYNC@@QAEAAU01@ABU01@@Z) referenced in function "public: struct VERTEX_TYPE & __thiscall VERTEX_TYPE::operator=(struct VERTEX_TYPE const &)" (??4VERTEX_TYPE@@QAEAAU0@ABU0@@Z)

这基本上归结为, VERTEX_TYPE::operator= 正在尝试使用SYNC::Vector2::operator=(const SYNC::Vector2 &)SYNC::Vector4::operator=(SYNC::Vector2 &)找不到定义。问题在于,1) 我从未在 VERTEX_TYPE 中声明、定义或使用赋值运算符,2) 即使我有,这些函数确实是在 .cpp 中定义的。这里自己看看。这是两个有问题的结构及其定义。

SYNC_Vectors.h

#ifndef SYNC_VECTORS_H
#define SYNC_VECTORS_H

#include <cmath>

namespace SYNC
{
    struct Vector2
    {
        Vector2();
        Vector2(const Vector2 & vec);
        Vector2(const float & x, const float & y);
        ~Vector2();

        inline Vector2 & operator=(const Vector2 & rhs);

        inline Vector2 operator+(Vector2 rhs);
        inline Vector2 operator-(Vector2 rhs);
        inline Vector2 operator*(const float & scalar);
        friend inline Vector2 operator*(const float & scalar, Vector2 rhs);

        inline Vector2 & operator+=(const Vector2 & rhs);
        inline Vector2 & operator-=(const Vector2 & rhs);
        inline Vector2 & operator*=(const float & scalar);

        bool operator==(const Vector2 & rhs);
        bool operator!=(const Vector2 & rhs);

        inline Vector2 & operator++();
        inline Vector2 & operator--();

        inline void Normal(Vector2 & rhs);

        Vector2 & Normalize();
        void Normalize(Vector2 & rhs);

        Vector2 & Dot(const Vector2 & rhs1, const Vector2 & rhs2);
        static float Cross(const Vector2 & lhs, Vector2 & rhs);

        float x;
        float y;
    };

    struct Vector3
    {
        Vector3();
        Vector3(const Vector3 & vec);
        Vector3(const float & x, const float & y, const float & z);
        virtual ~Vector3();

        inline Vector3 & operator=(const Vector3 & rhs);

        inline Vector3 operator+(Vector3 rhs);
        inline Vector3 operator-(Vector3 rhs);
        inline Vector3 operator*(const float & scalar);
        friend inline Vector3 operator*(const float & scalar, Vector3 rhs);

        inline Vector3 & operator+=(const Vector3 & rhs);
        inline Vector3 & operator-=(const Vector3 & rhs);
        inline Vector3 & operator*=(const float & rhs);

        inline bool operator==(const Vector3 & rhs);
        inline bool operator!=(const Vector3 & rhs);

        inline Vector3 & operator++();
        inline Vector3 & operator--();

        void Normalize();
        void Normalize(Vector3 rhs);

        void Dot(const Vector3 & vec1, const Vector3 & vec2);
        void Cross(const Vector3 & vec1, const Vector3 & vec2);

        float x;
        float y;
        float z;
    };

    struct Vector4
    {
        Vector4();
        Vector4(const Vector4 & rhs);
        Vector4(const float & x, const float & y, const float & z, const float & w);
        ~Vector4();

        inline Vector4 & operator=(const Vector4 & rhs);

        inline Vector4 operator+(Vector4 rhs);
        inline Vector4 operator-(Vector4 rhs);
        inline Vector4 operator*(const float & scalar);
        friend inline Vector4 operator*(const float & scalar, Vector4 rhs);

        inline Vector4 & operator+=(const Vector4 & rhs);
        inline Vector4 & operator-=(const Vector4 & rhs);
        inline Vector4 & operator*=(const float & rhs);

        inline bool operator==(const Vector4 & rhs);
        inline bool operator!=(const Vector4 & rhs);

        inline Vector4 & operator++();
        inline Vector4 & operator--();

        float x;
        float y;
        float z;
        float w;
    };

    struct Quaternion
    {
        Quaternion();
        Quaternion(const Quaternion & rhs);
        Quaternion(const Vector3 & v, const float & w);
        ~Quaternion();

        inline Quaternion & operator=(const Quaternion & rhs);

        inline bool operator==(const Quaternion & rhs);
        inline bool operator!=(const Quaternion & rhs);

        inline Quaternion operator*(Quaternion rhs);
        inline Quaternion & mul(const Quaternion & rhs);

        inline void Conjugate();
        inline void Conjugate(Quaternion &);

        inline void Normalize();
        inline void Normalize(Quaternion &);

        Vector3 v;
        float w;
    };


}
#endif

SYNC_Vectors.cpp

#include "SYNC_Vectors.h"

//-----------------------------------------
// SYNC::Vector2 defintions
//-----------------------------------------

SYNC::Vector2::Vector2()
{
    x = 0;
    y = 0;
}

SYNC::Vector2::Vector2(const Vector2 & vec)
{
    x = vec.x;
    y = vec.y;
}

SYNC::Vector2::Vector2(const float & ix, const float & iy)
{
    x = ix;
    y = iy;
}

SYNC::Vector2::~Vector2()
{
}

SYNC::Vector2 & SYNC::Vector2::operator=(const SYNC::Vector2 & rhs)
{
    x = rhs.x;
    y = rhs.y;

    return *this;
}

SYNC::Vector2 SYNC::Vector2::operator+(SYNC::Vector2 rhs)
{
    rhs.x += x;
    rhs.y += y;

    return rhs;
}

SYNC::Vector2 SYNC::Vector2::operator-(SYNC::Vector2 rhs)
{
    rhs.x -= x;
    rhs.y -= y;

    return rhs;
}

SYNC::Vector2 SYNC::Vector2::operator*(const float & scalar)
{
    SYNC::Vector2 ret( x * scalar, y * scalar);

    return ret;
}

SYNC::Vector2 operator*(const float & scalar, SYNC::Vector2 rhs)
{
    rhs.x *= scalar;
    rhs.y *= scalar;

    return rhs;
}

SYNC::Vector2 & SYNC::Vector2::operator+=(const Vector2 & rhs)
{
    x += rhs.x;
    y += rhs.y;

    return *this;
}

SYNC::Vector2 & SYNC::Vector2::operator-=(const Vector2 & rhs)
{
    x -= rhs.x;
    y -= rhs.y;

    return *this;
}

SYNC::Vector2 & SYNC::Vector2::operator*=(const float & scalar)
{
    x *= scalar;
    y *= scalar;

    return *this;
}

bool SYNC::Vector2::operator==(const Vector2 & rhs)
{
    if(rhs.x == x && rhs.y == y)
        return true;
    else 
        return false;
}

bool SYNC::Vector2::operator!=(const Vector2 & rhs)
{
    if(rhs.x != x || rhs.y != y)
        return true;
    else
        return false;
}

SYNC::Vector2 & SYNC::Vector2::operator++()
{
    x++;
    y++;

    return *this;
}

SYNC::Vector2 & SYNC::Vector2::operator--()
{
    x--;
    y--;

    return *this;
}

void SYNC::Vector2::Normal(Vector2 & rhs)
{
    rhs.x = y;
    rhs.y = -x;
}

SYNC::Vector2 & SYNC::Vector2::Normalize()
{
    if(x > 0.000001 || y > 0.000001)
    {
        float length = sqrt((x * x) + (y * y));
        x /= length;
        y /= length;
    }
    else
    {
        x = 0;
        y = 0;
    }

    return *this;
}

void SYNC::Vector2::Normalize(Vector2 & rhs)
{
    if(x > 0.000001 || y > 0.000001)
    {
        float length = sqrt((x * x) + (y * y));
        rhs.x = x / length;
        rhs.y = y / length;
    }
    else
    {
        rhs.x = 0;
        rhs.y = 0;
    }
}

SYNC::Vector2 & SYNC::Vector2::Dot(const Vector2 & rhs1, const Vector2 & rhs2)
{
    x = rhs1.x * rhs2.x;
    y = rhs1.y * rhs2.y;

    return *this;
}

float SYNC::Vector2::Cross(const Vector2 & rhs1, Vector2 & rhs2)
{
    return ((rhs1.x * rhs2.y) - (rhs1.y * rhs2.x));
}


//-----------------------------------------
// SYNC::Vector3 defintions
//-----------------------------------------


SYNC::Vector3::Vector3()
{
    x = 0;
    y = 0;
    z = 0;
}

SYNC::Vector3::Vector3(const Vector3 & vec)
{
    x = vec.x;
    y = vec.y;
    z = vec.z;
}

SYNC::Vector3::Vector3(const float & ix, const float & iy, const float & iz)
{
    x = ix;
    y = iy;
    z = iz;
}

SYNC::Vector3::~Vector3()
{
}

SYNC::Vector3 & SYNC::Vector3::operator=(const Vector3 & rhs)
{
    x = rhs.x;
    y = rhs.y;
    z = rhs.z;

    return *this;
}

SYNC::Vector3 SYNC::Vector3::operator+(Vector3 rhs)
{
    rhs.x += x;
    rhs.y += y;
    rhs.z += z;

    return rhs;
}

SYNC::Vector3 SYNC::Vector3::operator-(Vector3 rhs)
{
    rhs.x -= x;
    rhs.y -= y;
    rhs.z -= z;

    return rhs;
}

SYNC::Vector3 SYNC::Vector3::operator*(const float & rhs)
{
    Vector3 ret(x * rhs, y * rhs, z * rhs);
    return ret;
}

SYNC::Vector3 operator*(const float & scalar, SYNC::Vector3 rhs)
{
    rhs.x *= scalar;
    rhs.y *= scalar;
    rhs.z *= scalar;

    return rhs;
}

SYNC::Vector3 & SYNC::Vector3::operator+=(const Vector3 & rhs)
{
    x += rhs.x;
    y += rhs.y;
    z += rhs.z;

    return *this;
}

SYNC::Vector3 & SYNC::Vector3::operator-=(const Vector3 & rhs)
{
    x -= rhs.x;
    y -= rhs.y;
    z -= rhs.z;

    return *this;
}

SYNC::Vector3 & SYNC::Vector3::operator*=(const float & rhs)
{
    x *= rhs;
    y *= rhs;
    z *= rhs;

    return *this;
}

bool SYNC::Vector3::operator==(const Vector3 & rhs)
{
    if(x == rhs.x && y == rhs.y && z == rhs.z)
        return true;
    else 
        return false;
}

bool SYNC::Vector3::operator!=(const Vector3 & rhs)
{
    if(x != rhs.x || y != rhs.y || z != rhs.z)
        return true;
    else 
        return false;
}

SYNC::Vector3 & SYNC::Vector3::operator++()
{
    x++;
    y++;
    z++;

    return *this;
}

SYNC::Vector3 & SYNC::Vector3::operator--()
{
    x--;
    y--;
    z--;

    return *this;
}

void SYNC::Vector3::Normalize()
{
    if(x > 0.000001 || y > 0.000001 || z > 0.000001)
    {
        float length = sqrt((x * x) + (y * y) + (z * z));
        x /= length;
        y /= length;
        z /= length;
    }
    else
    {
        x = 0;
        y = 0;
        z = 0;
    }
}

void SYNC::Vector3::Normalize(Vector3 rhs)
{
    if(x > 0.000001 || y > 0.000001 || z > 0.000001)
    {
        float length = sqrt((x * x) + (y * y) + (z * z));
        rhs.x /= length;
        rhs.y /= length;
        rhs.z /= length;
    }
    else
    {
        rhs.x = 0;
        rhs.y = 0;
        rhs.z = 0;
    }
}

void SYNC::Vector3::Dot(const Vector3 & vec1, const Vector3 & vec2)
{
    x = vec1.x * vec2.x;
    y = vec1.y * vec2.y; 
    z = vec1.z * vec2.z;
}

void SYNC::Vector3::Cross(const Vector3 & vec1, const Vector3 & vec2)
{
    x = ((vec1.y * vec2.z) - (vec1.z * vec2.y));
    y = ((vec1.z * vec2.x) - (vec1.x * vec2.z));
    z = ((vec1.x * vec2.y) - (vec1.y * vec2.x));
}

//-----------------------------------------
// SYNC::Vector4 defintions
//-----------------------------------------

SYNC::Vector4::Vector4()
{
    x = 0;
    y = 0;
    z = 0;
    w = 0;
}

SYNC::Vector4::Vector4(const Vector4 & rhs)
{
    x = rhs.x;
    y = rhs.y;
    z = rhs.z;
    w = rhs.w;

}

SYNC::Vector4::Vector4(const float & ix, const float & iy, const float & iz, const float & iw)
{
    x = ix;
    y = iy;
    z = iz;
    w = iw;
}

SYNC::Vector4::~Vector4()
{
}

SYNC::Vector4 & SYNC::Vector4::operator=(const Vector4 & rhs)
{
    x = rhs.x;
    y = rhs.y;
    z = rhs.z;
    w = rhs.w;

    return *this;
}

SYNC::Vector4 SYNC::Vector4::operator+(Vector4 rhs)
{
    rhs.x += x;
    rhs.y += y;
    rhs.z += z;
    rhs.w += w;

    return rhs;
}

SYNC::Vector4 SYNC::Vector4::operator-(Vector4 rhs)
{
    rhs.x += x;
    rhs.y += y;
    rhs.z += z;
    rhs.w += w;

    return rhs;
}

SYNC::Vector4 SYNC::Vector4::operator*(const float & rhs)
{
    Vector4 ret( x * rhs, y * rhs, z * rhs, w * rhs);
    return ret;
}

SYNC::Vector4 operator*(const float & scalar, SYNC::Vector4 rhs)
{
    rhs.x *= scalar;
    rhs.y *= scalar;
    rhs.z *= scalar;
    rhs.w *= scalar;

    return rhs;
}

SYNC::Vector4 & SYNC::Vector4::operator+=(const Vector4 & rhs)
{
    x += rhs.x;
    y += rhs.y;
    z += rhs.z;
    w += rhs.w;

    return *this;
}

SYNC::Vector4 & SYNC::Vector4::operator-=(const Vector4 & rhs)
{
    x += rhs.x;
    y += rhs.y;
    z += rhs.z;
    w += rhs.w;

    return *this;
}

SYNC::Vector4 & SYNC::Vector4::operator*=(const float & rhs)
{
    x *= rhs;
    y *= rhs;
    z *= rhs;
    w *= rhs;
}

bool SYNC::Vector4::operator==(const Vector4 & rhs)
{
    if(x == rhs.x && y == rhs.y && z == rhs.z && w == rhs.w)
        return true;
    else
        return false;
}

bool SYNC::Vector4::operator!=(const Vector4 & rhs)
{
    if(x != rhs.x || y != rhs.y || z != rhs.z || w != rhs.w)
        return true;
    else
        return false;
}

SYNC::Vector4 & SYNC::Vector4::operator++()
{
    x++;
    y++;
    z++;
    w++;
}

SYNC::Vector4 & SYNC::Vector4::operator--()
{
    x--;
    y--;
    z--;
    w--;
}

//---------------------------------
// SYNC::Quaternion definitions
//---------------------------------

SYNC::Quaternion::Quaternion()
{
    v.x = 0;
    v.y = 0;
    v.z = 0;
    w = 0;
}

SYNC::Quaternion::Quaternion(const Quaternion & rhs)
{
    v.x = rhs.v.x;
    v.y = rhs.v.y;
    v.z = rhs.v.z;
    w = rhs.w;
}

SYNC::Quaternion::Quaternion(const Vector3 & iv, const float & iw)
{
    v = iv;
    w = iw;
}

SYNC::Quaternion::~Quaternion()
{
}

SYNC::Quaternion & SYNC::Quaternion::operator=(const Quaternion & rhs)
{
    v = rhs.v;
    w = rhs.w;
}

bool SYNC::Quaternion::operator==(const Quaternion & rhs)
{
    if(v == rhs.v && w == rhs.w)
        return true;
    else
        return false;
}

bool SYNC::Quaternion::operator!=(const Quaternion & rhs)
{
    if(v != rhs.v || w != rhs.w)
        return true;
    else
        return false;
}

SYNC::Quaternion SYNC::Quaternion::operator*(Quaternion rhs)
{
    rhs.v.x = (w * rhs.v.x) + (v.x * rhs.w)  + (v.y * rhs.v.z) - (v.z * rhs.v.y);
    rhs.v.y = (w * rhs.v.y) - (v.x * rhs.v.z) + (v.y * rhs.w) + (v.z * rhs.v.x);
    rhs.v.z = (w * rhs.v.z) + (v.x * rhs.v.y) - (v.y * rhs.v.x) + (v.z * rhs.w);
    rhs.w = (w * rhs.w) - (v.x * rhs.v.x) - (v.y * rhs.v.y) - (v.z * rhs.v.z); 

    return rhs;
}

SYNC::Quaternion & SYNC::Quaternion::mul(const Quaternion & rhs)
{
    v.x = (w * rhs.v.x) + (v.x * rhs.w)  + (v.y * rhs.v.z) - (v.z * rhs.v.y);
    v.y = (w * rhs.v.y) - (v.x * rhs.v.z) + (v.y * rhs.w) + (v.z * rhs.v.x);
    v.z = (w * rhs.v.z) + (v.x * rhs.v.y) - (v.y * rhs.v.x) + (v.z * rhs.w);
    w = (w * rhs.w) - (v.x * rhs.v.x) - (v.y * rhs.v.y) - (v.z * rhs.v.z); 

    return *this;
}

void SYNC::Quaternion::Conjugate()
{
    v *= -1;
}

void SYNC::Quaternion::Conjugate(Quaternion & rhs)
{
    rhs.v = v * -1;
    rhs.w = w;
}

void SYNC::Quaternion::Normalize()
{
    float length = sqrt((w*w) + (v.x * v.x) + (v.y * v.y) + (v.z * v.z));
    if(length > 0.000001)
    {
        v.x /= length;
        v.y /= length;
        v.z /= length;
        w /= length;
    }
    else
    {
        v.x = 0;
        v.y = 0;
        v.z = 0;
        w = 0;
    }
}

void SYNC::Quaternion::Normalize(Quaternion & rhs)
{
    float length = sqrt((w*w) + (v.x * v.x) + (v.y * v.y) + (v.z * v.z)); 
    if(length > 0.000001)
    {
        rhs.v.x = v.x / length;
        rhs.v.y = v.y / length;
        rhs.v.z = v.z / length;
        rhs.w = w / length;
    }
    else
    {
        rhs.v.x = 0;
        rhs.v.y = 0;
        rhs.v.z = 0;
        rhs.w = 0;
    }
}

同步模块

#ifndef SYNCMOD_H
#define SYNCMOD_H

#include <fstream>
#include <map>
#include <string>
#include "SYNC_Vectors.h"

struct SYNCMODEL_HEADER
{   
    char id[8];
    short ver[2];
    long m_numOfVertices;
    long m_numOfIndices;
    std::string m_modelName;
};

struct VERTEX_TYPE
{
    SYNC::Vector3 position;
    SYNC::Vector4 color;
    SYNC::Vector3 normal;
    SYNC::Vector3 binormal;
    SYNC::Vector3 tangent;
    SYNC::Vector2 textureCoords;
};

class SYNCMODEL_MATERIAL_HEADER
{
    enum DATA_TYPE{MATERIAL_SHORT , MATERIAL_INT, MATERIAL_LONG, MATERIAL_FLOAT, MATERIAL_DOUBLE};
    struct Data_Index
    {
        DATA_TYPE type;
        char * accessor;
    };

    int m_numOfElements;
    std::map<std::string, Data_Index> m_Indices;

};

std::ifstream & operator>>(std::ifstream & stream, SYNCMODEL_HEADER & header);

#endif

同步模块.cpp

#include "syncmod.h"

std::ifstream & operator>>(std::ifstream & stream, SYNCMODEL_HEADER & header)
{
    stream.read(header.id, 8);
    stream.read(reinterpret_cast<char *>(&header.ver), sizeof(short) * 2);
    stream.read(reinterpret_cast<char *>(&header.m_numOfVertices), sizeof(long));
    stream.read(reinterpret_cast<char *>(&header.m_numOfIndices), sizeof(long));
    std::getline(stream, header.m_modelName, '\0');
    stream.seekg(static_cast<int>(stream.tellg()) - 1);
    return stream;
}

有谁知道这到底是怎么回事?

编辑:这里只是一个额外的观察,为什么它只用这两个的赋值运算符而不是 SYNC::Vector3 来抛出标志?

4

1 回答 1

2

标头SYNC_Vectors.h声明:

inline Vector2 & operator=(const Vector2 & rhs);

源文件SYNC_Vectors.cpp定义:

SYNC::Vector2 & SYNC::Vector2::operator=(const SYNC::Vector2 & rhs)

从声明中删除inline,事情应该会变得更好。或者,正如我们所讨论的,通过复制文本或在文件末尾使用指令<g>将内联函数的定义放入. 对于后者,大多数人经常使用独特的扩展名。SYNC_Vectors.h#include.inl

于 2012-10-01T20:37:17.707 回答