English 中文(简体)
错误:在D方案拟订中发生范围违反
原标题:Error: range violation in D programming
  • 时间:2010-12-03 00:38:46
  •  标签:
  • arrays
  • d

我在结构上有动态阵列,而且采用动态阵列的方法。 问题在于,在我管理该方案时,我会遇到一系列侵权错误。 然而,当我在该方法内建立一个新的动态阵列时,该阵列却行不通。 以下法规造成问题。

struct MyStr {
 int[] frontArr;

    this(int max = 10) {
         frontArr = new int[10];
    }

    void push(int x) {
         frontArr[0] = x;
    }
}

void main() {
    MyStr s;
    s.push(5);
}

然而,这一种工程是:

struct MyStr {
 int[] frontArr;

    this(int max = 10) {
         frontArr = new int[10];
    }

    void push(int x) {
         frontArr = new int[10]; // <---Add this line
         frontArr[0] = x;
    }
}

void main() {
    MyStr s;
    s.push(5);
}

我基本上添加这一条,以检验范围。 似乎像最初的“前逃亡者”方法一样,在推进(int x)方法中就可以看到。 任何解释?

提前感谢。

最佳回答

必须保证启动建筑。 这是你不想为 exception而建造一座屏障。 因此,D不支持建筑构造中的违约构造。 想象

MyStr s;

结果是出现了一个例外。 而D则提供自己的违约建筑商,这些建筑商将所有领域都初步归为不动产。 在你的情况下,你不叫你施工者,而只是使用规定的缺省,即前线。 傲慢从没有开始。 你们想要的是:

void main() {
    MyStr s = MyStr(10);
    s.push(5);
}

也许应当是一种汇编错误,对构造的所有参数设定默认值。

问题回答

我会错过(在这样的情况下,我使用的是D,不过是一阵,在你的法典样本中,你试图给它一个阵列点。 动态阵列工作,如(注抄录了一个D tutorial 发现here

int[] MyArray;
MyArray.length = 3;

无论出于何种原因,D并不支持不需要争论的建筑构造,要么使用“眼镜”,要么在<条码>上删除“缺损初始器”()。

struct MyStr {
    int[] frontArr;

    static MyStr opCall() {
        MyStr s;
        s.frontArr = new int[10];
        return s;
    }

    void push(int x) {
        frontArr[0] = x;
    }
}




相关问题
WordPress Data Storage Efficiency

I ve been asked to review a WordPress plugin of sorts and try to find ways of making it faster. The premise of this plugin is basically to store a bunch of users and shifts and appointments and ...

Convert a 2D array index into a 1D index

I have two arrays for a chess variant I am coding in java...I have a console version so far which represents the board as a 1D array (size is 32) but I am working on making a GUI for it and I want it ...

Convert an array of integers for use in a SQL "IN" clause

Surely there is a framework method that given an array of integers, strings etc converts them into a list that can be used in a SQL "IN" clause? e.g. int[] values = {1,2,3}; would go to "(1,2,3)"

Sorting twodimensional Array in AS3

So, i have a two-dimensional Array of ID s and vote count - voteArray[i][0] = ID, voteArray[i][1] = vote count I want the top 3 voted items to be displayed in different colors, so i have a 2nd Array -...

C++ Array Sort Me

Stuck on an array sorter. Have to sort numbers from largest to smallest. I m trying two loops (one nested in the other). Here s the code: int counter=0; // inner counter int counter2=0; // outer ...

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

Best practice of big javascript objects

sry for this imprecise topic name. I am querying a dataset a lot of times so using ajax request would end up in tons of http requests. For this reason I decided to use the json encode method to ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

热门标签