2016-08-22 59 views
-3

我想編寫一個光線跟蹤和我不能編譯,因爲下面的錯誤我的程序:不匹配調用函數C++

src\util\Ray.cpp: In constructor 'Ray::Ray()': src\util\Ray.cpp:8:17: error: no match for call to '(Vector3D) (double, double, double)'
o(0.0, 0.0, 0.0); ^makefile.mak:31: recipe for target 'Ray.o' failed mingw32-make: *** [Ray.o] Error 1

這是代碼:

//Vector3D.h 
#ifndef __VECTOR3D__ 
#define __VECTOR3D__ 

class Vector3D{ 
    public: 

     float x; 
     float y; 
     float z; 

    public: 

     Vector3D(void); 
     Vector3D(const float&, const float&, const float&); 
     Vector3D(const Vector3D& obj); 
}; 

#endif 



//Vector3D.cpp 
#include <iostream> 

#include "Vector3D.h" 

using namespace std; 

Vector3D::Vector3D(void){ 
    x  = 0.0; 
    y  = 0.0; 
    z  = 0.0; 
} 

Vector3D::Vector3D(const float &p_x, const float &p_y, const float &p_z){ 
    x  = p_x; 
    y  = p_y; 
    z  = p_z; 
} 

Vector3D::Vector3D(const Vector3D& obj){ 
    x  = obj.x; 
    y  = obj.y; 
    z  = obj.z; 
} 



//Ray.h 
#ifndef __RAY__ 
#define __RAY__ 

#include "Vector3D.h" 

class Ray{ 
    public: 

     Vector3D o; 
     Vector3D d; 

    public: 

     Ray(void); 
}; 

#endif 



//Ray.cpp 
#include "Ray.h" 

Ray::Ray(void){ 
    o(0.0, 0.0, 0.0); 
} 

我不知道這裏有什麼錯,有人可以解釋嗎?

+3

也許'這個 - > O =的Vector3D(0.0,0.0,0.0);',而不是'Ø (0.0,0.0,0.0);'? – DimChtz

+0

作品,謝謝!你願意詳細說明爲什麼我必須這樣做嗎? – krbu

+1

OT:你的守衛是非法的。 https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-ac-identifier –

回答

1

當你到達構造函數的正文時,所有成員都已經被初始化。這意味着,在您的Ray構造:

Ray::Ray(void){ 
    o(0.0, 0.0, 0.0); 
} 

o(0.0, 0.0, 0.0);相當於o.operator()(0.0, 0.0, 0.0)

要調用非默認的構造函數的一員,你需要使用初始化列表:

Ray::Ray() : o(0.0, 0.0, 0.0) { 
    // note the body of the constructor is now empty 
}