How to Instantiate struct defined in template
Craig Dillabaugh
cdillaba at cg.scs.carleton.ca
Sat Sep 22 20:49:06 PDT 2012
Hello,
I am trying to figure out how templates work and tried to define
the
following template to define vertices of any dimension and
operations
on them.
import std.stdio;
import std.random;
import std.range;
import std.conv;
import std.math;
/*
* T must be one of the floating point types
* float, double, real _ I should be enforcing this.
*/
template vt(T)
{
struct vertex {
this(T[] crds...) {
if( crds.length < 2 ) {
throw new.Exception("To small!");
} else {
coords.length = crds.length;
foreach(ref v_coord, in_coord; lockstep(coords, crds))
{
v_coord = in_coord;
}
}
}
@property T x() { return coords[0];}
@property T y() { return coords[1];}
@property T z() { return ( coords.length < 3 ? T.nan :
coords[2] ); }
}
T euclid_dist(T a, T b) {
T sum = 0;
foreach( ref a_crd, ref b_crd; lockstep( a.coords, b.coords )
) {
sum += (a_crd - b_crd)*(a_crd - b_crd );
}
return sqrt(sum);
}
} //End of template
int main(string argv[] ) {
vt!(float).vertex v1(0.0,0.0);
vt!(float).vertex v2(3.0,4.0);
writefln("The distance between vertex 1 and vertex 2 is ",
vt!(float).euclid_dist(v1, v2) );
}
When I try to compile this I get the message:
vertex.d(57): Error: found 'v1' when expecting ';' following
statement
vertex.d(58): Error: found 'v2' when expecting ';' following
statement
Lines 57 and 58 are the first two lines in my main() function. I
can't figure out why, from the examples I've looked at I am using
the syntax properly, but am clearly missing something.
Also, since I don't really know what I am doing any criticisms on
how I am defining the vertex template are welcome.
Cheers,
Craig
More information about the Digitalmars-d-learn
mailing list