我正在关注YouTube tutorial to build an FPS game in C++,但我遇到了一个我无法解决的错误;链接器错误LNK2019:
错误1错误LNK2019:未解析的外部符号“public:__thiscall Vector3d :: ~Vector3d(void)”(?? 1Vector3d @@ QAE @ XZ)在函数“class std :: basic_ostream&gt;&amp; __cdecl operator&lt; &lt;(class std :: basic_ostream&gt;&amp;,class Vector3d)“(?? 6 @ YAAAV?$ basic_ostream @ DU?$ char_traits @ D @ std @@@ std @@ AAV01 @ VVector3d @@@ Z)<登记/> c:\ Users \ daniel \ documents \ visual studio 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;
}
答案 0 :(得分: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
中的声明。
答案 1 :(得分:2)
您的.h文件中声明了析构函数,但未在cpp中实现
~Vector3d(void); // you probably want to remove that line or default it if using C++11
声明一个空的析构函数首先是无用的,因为编译器无论如何都可以为你编写它,但更重要的是,因为C ++ 11它会阻止编译器生成一个移动构造函数,这将影响性能。