简体   繁体   English

未解析的函数未解决的外部符号链接器错误?

[英]Unresolved external symbol linker error for a function not even declared?

normally when i get this error, I go looking for a function i forgot to define, but i'm getting this from the assignment operator of a class, and i never declared it. 通常当我得到这个错误时,我会去找一个我忘了定义的函数,但是我从一个类的赋值运算符得到这个,我从来没有声明它。 The error goes like this: 错误是这样的:

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)

which essentially boils down to, VERTEX_TYPE::operator= is trying to use SYNC::Vector2::operator=(const SYNC::Vector2 &) and SYNC::Vector4::operator=(SYNC::Vector2 &) but cannot find the defintions. 其实基本归结为,VERTEX_TYPE :: operator =试图使用SYNC::Vector2::operator=(const SYNC::Vector2 &)SYNC::Vector4::operator=(SYNC::Vector2 &)但找不到定义。 The problem with this is, 1) I never declared, defined, or used the assignment operator in VERTEX_TYPE, 2) even if i had, those functions are indeed defined within the .cpp. 这个问题是,1)我从未在VERTEX_TYPE中声明,定义或使用赋值运算符,2)即使我有,这些函数确实在.cpp中定义。 Here see for yourself. 在这里看看你自己。 These are the two offending structs and their definitions. 这是两个违规结构及其定义。

SYNC_Vectors.h 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 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;
    }
}

syncmod.h syncmod.h

#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

syncmod.cpp syncmod.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;
}

anyone know what the heck is going on here? 有谁知道这里到底发生了什么?

edit: Just an additional observation here, why is it throwing up flags with just the assignment operator of these two and not SYNC::Vector3? 编辑:这里只是另外一个观察,为什么它只抛出这两个赋值运算符的标志而不是SYNC :: Vector3?

The header SYNC_Vectors.h declares: 标头SYNC_Vectors.h声明:

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

and the source file SYNC_Vectors.cpp defines: 和源文件SYNC_Vectors.cpp定义:

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

Remove the inline from the declaration and things should get better. 从声明中删除inline ,事情应该会变得更好。 <g> Or, as we've discussed, put the definitions of the inline functions into SYNC_Vectors.h , either by copying the text or with a #include directive at the end of the file. <g>或者,正如我们所讨论的,将内联函数的定义放入SYNC_Vectors.h ,方法是复制文本或在文件末尾使用#include指令。 For the latter, most people use a distinctive extension, often .inl . 对于后者,大多数人使用独特的扩展,通常是.inl

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 内联函数的链接器未解析的外部符号 - Linker unresolved external symbol for inline function 链接器错误“未解析的外部符号”:使用模板 - Linker error 'unresolved external symbol' : working with templates 未解析的外部符号链接器错误(C ++) - Unresolved External Symbol linker error (C++) 未解决的外部符号错误,即使已定义功能并被IDE看到也是如此 - Unresolved external symbol error, even if function is defined and seen by IDE 即使函数存在char *,仍然存在未解析的外部符号错误 - unresolved external symbol error even though the function exists char* 链接器错误-无法解析的外部符号 - Linker Errors - Unresolved external symbol 链接器错误“... .obj:错误 LNK2019:函数中引用的未解析的外部符号” - Linker error “… .obj : error LNK2019: unresolved external symbol referenced in function” Visual Studio 2015中无法解决的外部符号链接器错误 - Unresolved external symbol linker error in Visual Studio 2015 链接器错误未解析的外部符号与我的cnt变量 - Linker error unresolved external symbol with my cnt variable Linker 使用其他 class 的构造函数时出现“无法解析的外部符号”错误 - Linker Error “unresolved external symbol” when using constructor of other class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM