我是C/C++的新手,嘗試對Point
和RectBound
的結構進行模板化,以允許使用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;
但這並沒有幫助。
有什麼建議嗎?
點需要與模板參數進行初始化,如內聯BOOL包含(點&p),但爲什麼你需要一個獨特的類型,每個維度是超越我 –
IdeaHat
的http:// ideone .com/moACJh –
@MadScie nceDreams允許double,float和int。自從我們處理大量數據以來,它更像是一種節省空間的技術。如果我們不希望z維例如具有雙精度的話,這會產生很大的差異。此外,這是一個面向列的數據庫,所以每個維度都有自己的文件... –