1

我正在按照这个YouTube 教程用 C++ 构建一个 FPS 游戏,但我遇到了一个我无法解决的错误;链接器错误LNK2019

错误 1 ​​错误 LNK2019:函数“class std::basic_ostream > & __cdecl operator<<(class std: :basic_ostream > &,class Vector3d)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@VVector3d@@@Z)
c:\Users\daniel\documents\视觉工作室 2012\Projects\Project2\Project2\Vector3d.obj Project2

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

这是我Vector3d在中声明的类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;
}  
4

2 回答 2

4

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

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

但是您从未在任何地方实现它,您应该在以下位置添加一个实现Vector3d.cpp

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

或者Vector3d.h如果您没有要销毁的内容,请删除其中的声明。

于 2013-06-03T13:23:37.993 回答
2

您在 .h 文件中声明了一个析构函数,但未在 cpp 中实现

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

声明一个空的析构函数首先是没有用的,因为编译器无论如何都可以为你编写它,但更重要的是,从 C++11 开始,它会阻止编译器生成移动构造函数,这会影响性能。

于 2013-06-03T13:24:14.193 回答