当前位置: 首页 > 知识库问答 >
问题:

错误链接2019 c

弘焕
2023-03-14

我跟随YouTube教程用C构建FPS游戏,但遇到了一个无法解决的错误;链接器错误LNK2019:

错误1错误LNK2019:未解析的外部符号"public:__thiscallVector3d::~Vector3d(val)"(??1Vector3d@@QAE@XZ)引用在函数"class std::basic_ostream

我需要做什么来解决这个问题?

这是我在Vector3d中声明的类。h:

#ifndef VEC_H
#define VEC_H

#include<iostream>
#include<cmath>

class Vector3d
{
  public:
    float x,y,z;
    Vector3d(void);
    Vector3d(float,float);
    Vector3d(float,float,float);
    ~Vector3d(void);

    float dotPrudect(const Vector3d& vec2);
    Vector3d crossproduct(const Vector3d& vec2);

    float lenght();
    void normalize();

    void change(float a,float b,float c);
    void change(Vector3d& vec2);
    void change(Vector3d vec2);
    void changeX(float a);
    void changeY(float b);
    void changeZ(float c);

    Vector3d operator+(const Vector3d& vec2);
    Vector3d operator-(const Vector3d& vec2);
    Vector3d operator*(const float& num);
    Vector3d operator/(const float& num);
    Vector3d& operator+=(const Vector3d& vec2);
    Vector3d& operator-=(const Vector3d& vec2);
    Vector3d& operator*=(const float& num);
    Vector3d& operator/=(const float& num);
    bool operator==(const Vector3d& vec2);
    bool operator!=(const Vector3d& vec2);
    friend std::ostream& operator<<(std::ostream& out,Vector3d vec2);
};

#endif

以及对应的Vector3d.cpp实现:

#include "Vector3d.h"

Vector3d::Vector3d()
{
    x=y=z=0;
}

Vector3d::Vector3d(float a,float b)
{
    x=a;
    y=b;
    z=0;
}

Vector3d::Vector3d(float a,float b,float c)
{
    x=a;
    y=b;
    z=c;
}

float Vector3d::dotPrudect(const Vector3d& vec2)
{
    return (x*vec2.x+y*vec2.y+z*vec2.z);
}

Vector3d Vector3d::crossproduct(const Vector3d& vec2)
{
    return (Vector3d(y*vec2.z-z*vec2.y,x*vec2.z-z*vec2.x,x*vec2.y-y*vec2.x));
}

float Vector3d::lenght()
{
    return (sqrt(x*x+y*y+z*z));
}

void Vector3d::change(float a,float b,float c)
{
    x=a;
    y=b;
    z=c;
}

void Vector3d::change(Vector3d vec)
{
    x=vec.x;
    y=vec.y;
    z=vec.z;
}

void Vector3d::change(Vector3d& vec)
{
    x=vec.x;
    y=vec.y;
    z=vec.z;
}

void Vector3d::changeX(float a)
{
    x=a;
}

void Vector3d::changeY(float b)
{
    y=b;
}

void Vector3d::changeZ(float c)
{
    z=c;
}


void Vector3d::normalize()
{
    float len=lenght();
    if(len!=0)
    {
        x/=len;
        y/=len;
        z/=len;
    }
}

Vector3d Vector3d::operator+(const Vector3d& vec2)
{
    return (Vector3d(x+vec2.x,y+vec2.y,z+vec2.z));
}

Vector3d Vector3d::operator-(const Vector3d& vec2)
{
    return (Vector3d(x-vec2.x,y-vec2.y,z-vec2.z));
}

Vector3d Vector3d::operator*(const float& num)
{
    return (Vector3d(x*num,y*num,z*num));
}

Vector3d& Vector3d::operator+=(const Vector3d& vec2)
{
    x+=vec2.x;
    y+=vec2.y;
    z+=vec2.z;
    return *this;
}

Vector3d& Vector3d::operator-=(const Vector3d& vec2)
{
    x-=vec2.x;
    y-=vec2.y;
    z-=vec2.z;
    return *this;
}

Vector3d& Vector3d::operator*=(const float& num)
{
    x*=num;
    y*=num;
    z*=num;
    return *this;
}

Vector3d& Vector3d::operator/=(const float& num)
{
    if(num!=0)
    {
        x/=num;
        y/=num;
        z/=num;
    }
    return *this;
}

bool Vector3d::operator==(const Vector3d& vec2)
{
    return (x==vec2.x && y==vec2.y && z==vec2.z);
}

bool Vector3d::operator!=(const Vector3d& vec2)
{
    return (x!=vec2.x && y!=vec2.y && z!=vec2.z);
}

std::ostream& operator<<(std::ostream& out,Vector3d vec2)
{
    out << vec2.x << "\t" << vec2.y << "\t" << vec2.z << std::endl;
    return out;
}  

共有2个答案

欧阳安阳
2023-03-14

在中声明了析构函数。h文件,但未在cpp中实现

~Vector3d(void); // you probably want to remove that line or default it if using C++11

声明空析构函数首先是无用的,因为编译器无论如何都可以为您编写它,但更重要的是,由于C 11,它将阻止编译器生成移动构造函数,这将影响性能。

封昊天
2023-03-14

您在头文件中声明了一个析构函数:

class Vector3d
{
  public:
    float x,y,z;
    Vector3d(void);
    Vector3d(float,float);
    Vector3d(float,float,float);
    ~Vector3d(void);
    ^^^^^^^^^^^^^^^^
    ...

但您从未在任何地方实现过它,您应该在Vector3d中添加一个实现。cpp:

Vector3d::~Vector3d()
{
   ...
}

或者删除Vector3d中的声明。如果你没有东西要销毁。

 类似资料:
  • 我一直试图静态地将sfml链接到C++项目,但它总是给我带来编译器错误,有人知道这里发生了什么以及如何修复它吗; 编辑: 这是产生错误的代码: 严重性代码描述项目文件行抑制状态错误LNK2019未解析外部符号_JPEG_CreateCompress在函数“private:bool__thiscall SF::Priv::ImageLoader::WriteJPG(类STD::Basic_Strin

  • 我认为我的模板使用不当,但我不知道我做错了什么。这就像模板链表无法确定它需要使用我的术语类一样。 名单- 以下是Visual Studio 2012的确切错误: > 错误LNK1120:1未解决的外部C:\用户\迈克尔\文档\魔术公文包\尚普兰\课程工作\数据结构\pa2\调试\pa2.exe 标题。H 功能。cpp 链接列表。H 术语h

  • 为什么我会得到不满意的链接错误? 细节如下: 本机方法的原型及其在我的Java代码中的调用是这样的: 私有本机long sampleFunction(long[][]twoDimArray,long number); p.SampleFunction(twoDimArray,number); 本机方法在VC++代码中的原型是这样的: JNIEXPORT jlong JNICALL Java_MyC

  • 已成功创建nativetest.h文件 NativeTest.c代码 gcc-I/usr/java/jdk1.7.0_13/include-I/usr/java/jdk1.7.0_13/include/linux-o nativetest.so-shared nativetest.c 已成功创建共享对象文件。 当我执行nativetest时,它显示了以下错误 java-djava.library.

  • 我是c语言新手,我有以下问题: 文件:-main。cpp-实用程序。h-实用程序.cpp 当我在做: g-c-std=c 11实用程序。cpp(编译)g-c-std=c11main。cpp(编译) 当我尝试链接时: g -o main.o utils.o /usr/lib/gcc/i686 redhat-linux/4.8.3/../../../crt1.o:在函数main'utils的引用。o:

  • 嘿,伙计们,我在安装了react native payola包装后制作了一个react native应用程序,我面临这个问题 如何解决这些错误......