October 04, 2017 conversion error related to array index | ||||
---|---|---|---|---|
| ||||
Hi, I get the following compile error with this function below: source\mod_data\matrices.d(66,12): Error: cannot implicitly convert expression (j) of type ulong to uint (66,12) is [j] in row[j] Using uint as type for rows, cols (i.e. the indices) works. Is ulong not allowed for array indices? I could not find the respective information. Thanks! 49 private double[][] matrix_uniform(ulong rows, ulong cols, double lbound, double ubound, ulong precision) 50 { 51 double[][] array; 52 double[] rowT; 53 auto gen = Random(unpredictableSeed); 54 auto rndUniform = uniform(lbound, ubound, gen); 55 56 foreach(ulong i; 0..cols) 57 { 58 rowT ~= 0; 59 } 60 61 foreach(ulong i; 0..rows) 62 { 63 foreach(ulong j; 0..cols) 64 { 65 rndUniform = uniform(lbound, ubound, gen); 66 rowT[j] = to!double(rndUniform) / 10^^precision; 67 } 68 array ~= rowT.dup; 69 } 70 return array; 71 } |
October 04, 2017 Re: conversion error related to array index | ||||
---|---|---|---|---|
| ||||
Posted in reply to thorstein | On Wednesday, October 04, 2017 09:04:10 thorstein via Digitalmars-d-learn wrote:
> Hi,
>
> I get the following compile error with this function below:
> source\mod_data\matrices.d(66,12): Error: cannot implicitly
> convert expression (j) of type ulong to uint
>
> (66,12) is [j] in row[j]
>
> Using uint as type for rows, cols (i.e. the indices) works. Is ulong not allowed for array indices? I could not find the respective information.
Array indices - and array length - are size_t. If you're on a 32-bit system, that's an alias to uint, whereas on a 64-bit system, it's an alias to ulong. In general, code should be using size_t when dealing with arrays not uint or ulong (unless you're talking about having elements of uint or ulong). If you don't use size_t, then you're likely to run into problems when compiling for a different architecture.
But if you have a ulong that you need to convert to a uint, then you can always cast or use std.conv.to!uint so long as the number will actually fit in a uint.
- Jonathan M Davis
|
Copyright © 1999-2021 by the D Language Foundation