2013-06-03 75 views
1

我正在關注這個YouTube tutorial to build an FPS game in C++,但我遇到了一個我無法解決的錯誤;鏈接器錯誤LNK2019錯誤鏈接2019 C++

錯誤1個錯誤LNK2019:解析外部符號 「公共:__thiscall的Vector3D ::〜的Vector3D(無效)」(?? 1Vector3d @@ @ QAE XZ)在函數「類的std ::引用basic_ostream > & __cdecl運算符< <(class std :: basic_ostream> &,class Vector3d)「(?? 6 @ YAAAV?$ basic_ostream @ DU?$ char_traits @ D @ std @@@ std @@ AAV01 @ VVector3d @@@ Z )
C:\用戶\丹尼爾​​\的文檔\ Visual Studio的 2012 \項目\ Project2的\ Project2的\ Vector3d.obj Project2的

我需要做些什麼來解決這個問題?

這是我Vector3dVector3d.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

不要隱藏鏈接後面的相關信息。您鏈接的頁面將會消失,您的問題將對其他人無用。 – Oswald

回答

2

你有一個析構函數在.h文件中聲明,但在CPP

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

聲明一個空的析構函數沒有實現首先是無用的,因爲編譯器可以反正重要的是寫你,但更因爲C語言++ 11它會阻止編譯器生成移動構造函數,這會影響性能。

+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如果你沒有給毀了。

+0

謝謝你的工作 –