Reputation: 9237
I am new to C/C++ and trying to template a struct of Point
and RectBound
to allow for both double and float types.
Here is the Point definition
// 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);
}
};
and here is the RectBound structure
// 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);
}
};
I get the following errors which are expected : (those from the return lines in RectBounds incline function)
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’
I tried to define a Point within RectBounds class as follows
Point<T,U,V> cp;
but that didn't help.
Any suggestions?
Upvotes: 0
Views: 213
Reputation: 110648
First of all, it seems that you've overcomplicating your templates. In a Point
, can x
, y
, and z
be different types? I'm guessing not, so you only need one template parameter:
template <typename T>
struct Point{
T x;
T y;
T z;
// ...
};
The same should be done for RectBounds
.
Now, the reason you're actually getting an error is due to the parameter types of your functions contains
and intersects
. You have used Point
and RectBounds
. However, these name templates, not types. Function parameters need to have types. Since we have given Point
only one template parameter now, a type might be Point<float>
. As you can see, we need to provide a template argument. So the following would work:
inline bool contains(Point<float> & p) {
However, it seems that you probably don't want to fix it to some specific type. You either want to accept only Point
s that have the same type as the template argument for RectBounds
:
inline bool contains(Point<T> & p) {
Or you want to be able to accept Point
s with arbitrary type, in which case you can make contains
a member function template:
template <typename U>
inline bool contains(Point<U> & p) {
Upvotes: 2