2014-01-22 76 views
0

我是C/C++的新手,嘗試對PointRectBound的結構進行模板化,以允許使用double和float類型。如何在另一個模板化結構中使用模板化結構(C++)

這裏是點定義

// simple Point structure               
template <typename T, typename U, typename V>          
struct Point{                  
    T x;                    
    U y;                    
    V z; // optional                 

    /*                    
    Point(){                   
     x = T();                  
     y = U();                  
     z = V();                  
    }*/                    

    Point(T _x, U _y) {                
     x = _x;                  
     y = _y;                  
    }                    

    Point(T _x, U _y, V _z){               
     x = _x;                  
     y = _y;                  
     z = _z;                  
    }                    

    inline bool equals(Point & p){             
     return(p.x == x && p.y == y);            
    }                    

}; 

這裏是RectBound結構

// Rectangular Bounds for tree 
// T,U should hold double or float only 
template <typename T, typename U, typename V> 
struct RectBounds { 
    // x, y center point 
    T x; 
    U y; 
    // dimension width w and height h 
    T w, h; 

    //constructors 

    // (_x, _y): center of rectangle bound. (_w, _h): width and height 
    RectBounds(T _x, U _y, T _w, T _h){ 
     x = _x; 
     y = _y; 
     w = _w; 
     h = _h; 
    } 

    // returns true if point p is in Rect bounds, false otherwise 
    inline bool contains(Point & p) { 
     float _w = w/2.0f; 
     float _h = h/2.0f; 
     return p.x >= x - _w && p.x < x + _w && p.y >= y - _h && p.y < y + _h; 
    } 

    // returns true if rectangle o intersects this, false otherwise 
    inline bool intersects(RectBounds & o){ 
     float _w = w/2.0f; 
     float _h = h/2.0f; 
     return !(o.y + o.h/2.0f <= y - _h || o.y - o.h/2.0f >= y + _h || o.x + o.w/2.0f <= x - _w || o.x - o.w/2.0f >= x + _w); 
    } 
}; 

我收到以下錯誤,這是預期:(那些從RectBounds傾斜函數的返回線)

error: request for member ‘x’ in ‘p’, which is of non-class type ‘int’ 
error: request for member ‘x’ in ‘p’, which is of non-class type ‘int’ 
error: request for member ‘y’ in ‘p’, which is of non-class type ‘int’ 
error: request for member ‘y’ in ‘p’, which is of non-class type ‘int’ 

我試圖在RectBounds類中定義一個Point,如下所示

Point<T,U,V> cp;但這並沒有幫助。

有什麼建議嗎?

+0

點需要與模板參數進行初始化,如內聯BOOL包含(點&p),但爲什麼你需要一個獨特的類型,每個維度是超越我 – IdeaHat

+0

的http:// ideone .com/moACJh –

+0

@MadScie nceDreams允許double,float和int。自從我們處理大量數據以來,它更像是一種節省空間的技術。如果我們不希望z維例如具有雙精度的話,這會產生很大的差異。此外,這是一個面向列的數據庫,所以每個維度都有自己的文件... –

回答

2

首先,你似乎已經過分複雜你的模板。在Point中,x,yz可以是不同的類型嗎?我猜沒有,所以你只需要一個模板參數:

template <typename T>          
struct Point{                  
    T x;                    
    T y;                    
    T z; 

    // ... 
}; 

同樣應RectBounds來完成。

現在,您實際發生錯誤的原因是由於您的函數containsintersects的參數類型。您已使用PointRectBounds。但是,這些名稱模板,而不是類型。函數參數需要有類型。由於我們現在只給出了Point只有一個模板參數,因此類型可能是Point<float>。正如你所看到的,我們需要提供一個模板參數。所以下面的工作:

inline bool contains(Point<float> & p) { 

但是,看起來你可能不想修復它的某些特定類型。要麼你想只接受Point S作相同類型的模板參數爲RectBounds

inline bool contains(Point<T> & p) { 

或者你希望能夠接受Point s的任意類型,在這種情況下,你可以讓contains成員函數模板:

template <typename U> 
inline bool contains(Point<U> & p) {