Thread overview | |||||
---|---|---|---|---|---|
|
September 25, 2004 (newb q.) how to use struct templates? | ||||
---|---|---|---|---|
| ||||
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. |
September 25, 2004 Re: (newb q.) how to use struct templates? | ||||
---|---|---|---|---|
| ||||
Posted in reply to clayasaurus | 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
|
September 25, 2004 Re: (newb q.) how to use struct templates? | ||||
---|---|---|---|---|
| ||||
Posted in reply to h3r3tic | h3r3tic wrote:
> 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
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 :)
|
Copyright © 1999-2021 by the D Language Foundation