digitalmars.D - (newb q.) how to use struct templates?
- clayasaurus (38/38) Sep 24 2004 Hello, I'm trying to convert my structure ...
- h3r3tic (38/39) Sep 24 2004 You might try this (compiles):
- clayasaurus (6/57) Sep 24 2004 cool thanks. it's working so far :)
Hello, I'm trying to convert my structure ...
struct Vector
{
x,y,z;
// Vector operator overloads
Vector opAdd(Vector argVec)
{ // +
Vector returnVec;
returnVec.initialize(x + argVec.x, y + argVec.y, z + argVec.z);
return returnVec;
}
}
into a template...
struct Vector(T)
{
T x,y,z;
// Vector operator overloads
Vector opAdd(Vector argVec)
{ // +
Vector returnVec;
returnVec.initialize(x + argVec.x, y + argVec.y, z + argVec.z);
return returnVec;
}
}
my problem is i've never used templates before and am confused on how
they work :-/
for example, is my opAdd template code valid?
also, how do you pass templates into functions and modify them?
I've tried ....
template getCross(vector1, vector2) // how do I specify the Vector template?
{
Vector normal;
normal.x = 0; // here I get an error saying "no identifier for
declarator"
return normal; // here it says "declaration expected, not 'return'"
}
again I'm confused on how templates are supposed to work :-/
i would like some help please, thanks.
Sep 24 2004
clayasaurus wrote:Hello, I'm trying to convert my structure ...You might try this (compiles): <code> struct Vector(T) { T x,y,z; static Vector opCall(T x, T y, T z) { Vector result; result.x = x; result.y = y; result.z = z; return result; } // Vector operator overloads Vector opAdd(Vector argVec) { // + return Vector(x + argVec.x, y + argVec.y, z + argVec.z); } } template getCross(T) // how do I specify the Vector template? { Vector!(T) getCross(Vector!(T) vector1, Vector!(T) vector2) { Vector!(T) normal; normal.x = 0; // here I get an error saying "no identifier for declarator" return normal; // here it says "declaration expected, not 'return'" } } void main() { Vector!(float) v1 = Vector!(float)(1, 2, 3); Vector!(float) v2 = Vector!(float)(2, 3, 4); Vector!(float) v3 = getCross!(float)(v1, v2); } </code> Tom
Sep 24 2004
h3r3tic wrote:clayasaurus wrote:cool thanks. it's working so far :) this is the first time i've ever used templates (they confuse me a bit so i never really bothered with them, until I found a practical use for them) Anyways, I'll post here again if/when my template struct is implemented :)Hello, I'm trying to convert my structure ...You might try this (compiles): <code> struct Vector(T) { T x,y,z; static Vector opCall(T x, T y, T z) { Vector result; result.x = x; result.y = y; result.z = z; return result; } // Vector operator overloads Vector opAdd(Vector argVec) { // + return Vector(x + argVec.x, y + argVec.y, z + argVec.z); } } template getCross(T) // how do I specify the Vector template? { Vector!(T) getCross(Vector!(T) vector1, Vector!(T) vector2) { Vector!(T) normal; normal.x = 0; // here I get an error saying "no identifier for declarator" return normal; // here it says "declaration expected, not 'return'" } } void main() { Vector!(float) v1 = Vector!(float)(1, 2, 3); Vector!(float) v2 = Vector!(float)(2, 3, 4); Vector!(float) v3 = getCross!(float)(v1, v2); } </code> Tom
Sep 24 2004








clayasaurus <clayasaurus gmail.com>