2013-05-02 64 views
-1

我一直在試圖解決最後一天左右的錯誤,似乎無法解決它。 這是一種錯誤,該修復可能很容易:S 我試圖尋找類似的問題,但修復不適用。錯誤:抽象類「球體」的對象不允許。和錯誤:沒有運算符「=」匹配這些操作數

的main.c

int main(int argc, char *argv[]){ 
    int width, height; 
    std::vector<Obj*> world; 

    world.push_back(new Sphere(Vec(0, 0, -22), 2, Vec(0.2, 0.2, 0.2), true)); 

    (...) 

    return 0; 
} 

當我嘗試創建一個球體被發現的錯誤。

相關類 Obj.h

class Obj 
{ 
public: 
    Vec color; 
    bool culling; 

    virtual bool intersect(const Vec &ray_orig, Vec &ray_dir, float *t0 = NULL, float *t1 = NULL) = 0; 
}; 

Sphere.h

class Sphere: public Obj 
{ 
public: 
    Vec center;       
    float radius, radius2;     

    Sphere(Vec center, float radius, Vec color, bool culling); 

    bool intersect(const Vec &ray_orig, Vec &ray_dir, float *t0 = NULL, float *t1 = NULL); 
}; 

Sphere.c

Sphere::Sphere(Vec center, float radius, Vec color, bool culling){ 
    this->center = center; 
    this->radius = radius; 
    this->color = color; 
    this->culling = culling; 
} 


bool Sphere::intersect(const Vec &ray_orig, Vec &ray_dir, float *t0 = NULL, float *t1 = NULL) {...} 

這些第二個錯誤出現在我做this-> color = color;。不知道他們是否有關係。

Vec是一個帶有3個變量的簡單結構。

如果您需要更多信息,我會盡快添加。

預先感謝您。

何塞

編輯

抱歉耽擱。

intersect(...)是obj類中唯一的函數。無論如何要保持抽象,因爲有幾個對象(sphere,box,...)。

如這裏要求去vec.h

vec.h

struct Vec { 

     float x, y, z; 

     Vec() {x = 0, y = 0, z = 0;} 
     Vec(float val) {x = val, y = val, z = val;} 
     Vec(float x_val,float y_val,float z_val) {x = x_val, y = y_val, z = z_val;} 
     Vec(const Vec& copy) : x(copy.x), y(copy.y), z(copy.z) { } 

     Vec operator+ (const Vec& p) const { 
      return Vec(x + p.x, y + p.y, z + p.z); 
     } 

     Vec operator- (const Vec& p) const { 
      return Vec(x - p.x, y - p.y, z - p.z); 
     } 

     Vec& operator += (const Vec& p) { 
      x += p.x; y += p.y; z += p.z; 
      return *this; 
     } 

     Vec& operator -= (const Vec& p) { 
      x -= p.x; y -= p.y; z -= p.z; 
      return *this; 
     } 

     Vec operator* (const float f) const { 
      return Vec(f * x, f * y, f * z); 
     } 

     Vec& operator*= (const float f) { 
      x *= f; y *= f; z *= f; 
      return *this; 
     } 

     Vec operator/ (const float f) const { 
      float inv = 1.f/f; 
      return Vec(inv * x, inv * y, inv * z); 
     } 

     Vec& operator/= (const float f) { 
      float inv = 1.f/f; 
      x *= inv; y *= inv; z *= inv; 
      return *this; 
     } 

Vec& operator= (const Vec& p) { 
     x = p.x; y = p.y; z = p.z; 
     return *this; 
    } 

    bool operator== (const Vec& p) { 
     if(x == p.x && y == p.y && z == p.z) 
      return true; 
     return false; 
    } 

     float length_squared() const { 
      return x*x + y*y + z*z; 
     } 

     float length() const { 
      return sqrt(length_squared()); 
     } 

     Vec norm() const { 
      float nor = x * x + y * y + z * z; 
      if (nor > 0) { 
       float invNor = 1/sqrt(nor); 
       (float)x *= invNor, (float)y *= invNor, (float)z *= invNor; 
      } 
      return *this; 
     } 

     Vec cross(const Vec&b) { 
      return Vec(y*b.z-z*b.y,z*b.x-x*b.z,x*b.y-y*b.x); 
     } 

     float dot(const Vec& v) { 
      return x * v.x + y * v.y + z * v.z; 
     } 


    }; 
+3

「抽象類」意味着「Obj」具有一個或多個「Sphere」不重疊的純虛函數。但是你發佈的代碼中唯一的虛函數並不純,而是被覆蓋。 'Obj'中是否還有其他虛擬函數沒有顯示給我們? – 2013-05-02 17:10:20

+1

第二個錯誤意味着'Vec'沒有可訪問的賦值操作符;我們需要查看'Vec'的定義來找出那裏有什麼問題。 – 2013-05-02 17:11:55

+2

[此代碼沒有給出您描述的錯誤],請製作[SSCCE](http://sscce.org/)(http://coliru.stacked-crooked.com/view?id=abb7b5b3230ceadc5d6bedd9111dc814-50d9cfc8a1d350e7409e81e87c2653ba) – 2013-05-02 17:12:24

回答

0

的定義看來問題確實在VEC文件。

找不到有關修復的推理,但將Vec結構更改爲類修復了所有問題。

感謝您的幫助。

相關問題