我正在按照这个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;
}