Template classes
Steven Schveighoffer
schveiguy at yahoo.com
Tue Apr 14 14:33:29 PDT 2009
On Tue, 14 Apr 2009 17:01:28 -0400, Andrew Spott <andrew.spott at gmail.com>
wrote:
> So, the attached is supposed to be a class that creates a vector of any
> type (I would like it to only take numerical values (int, float, real,
> double, etc), however, I am ok with it taking others (not that I see why
> someone would use it that way).
>
> I tried to compile it with the following, but it won't compile. I don't
> really understand the errors I'm getting either.
>
> Can someone tell me what I'm doing wrong? If you need more information,
> let me know.
>
> -Andrew
>
> void testvector() {
> auto v = new Vector!(float)(3, 12.0);
> writefln(v.toString());
> int n = 0;
>
> while (n < n.size()) {
> v[n] = n;
> }
>
> writefln(v.toString());
>
> }
Try the attached (untested).
You might get some insight into how d should be written by looking at the
changes.
BTW, if this is just an exercise fine, but your example is a completely
redundant implementation of standard builtin arrays.
e.g., the following code does almost the same thing you are doing without
requiring a new class:
void testvector() {
auto v = new float[3];
v[0..$] = 12.0;
writefln(v);
int n = 0;
while(n < v.length) { // bug in original code
v[n] = n;
n++; // bug in original code
}
writefln(v);
}
-Steve
-------------- next part --------------
A non-text attachment was scrubbed...
Name: vector.d
Type: application/octet-stream
Size: 505 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20090414/01f84557/attachment.obj>
More information about the Digitalmars-d-learn
mailing list