[疑难] 帮我除错
soulmachine
2007-09-29
有个小程序:
import std.string; const MaxListSize = 20; //the max size of the linelist1 struct STU { char[10] name; char[10] stuno; int age; int score; }; alias STU ElemType; class List //the definition of linelist { private: ElemType[MaxListSize] elem; //to store the elements int length;//current size int MaxSize;//max size public: //initialize the linelist this(int ms) { length = 0; MaxSize = ms; } //delete the head, tail or one element that equals to e bool listDelete(int mark, out ElemType e) { if(length == 0) return false; if(mark > 0) //delete the head { e = elem[0]; for(int i = 1; i < length; i++) elem[i - 1] = elem[i]; } else { if(mark < 0) e = elem[length - 1]; //delete the tail, else //delete the element which equals to e { int i; for(i = 0; i < length; i++) if(cmp(elem[i].name, e.name) == 0) break; if(i == length) return false; else { e = elem[i]; for(int j = i + 1; j < length; j++) elem[j - 1] = elem[j]; } } } --length; return true; } } 编译出错:test.d(38): Error: array 'length' hides other 'length' name in outer scope 但我怎么也找不出外出作用域有个length |
|
tomqyp
2007-09-29
文档里有说,如果后缀表达式 的类型为静态或者动态数组,会隐式地声明变量 length ,并将数组的长度赋给它。
试试这个就知道 void main() { char[] a = "12345"; char b = a[length-1]; assert(b == '5'); } |
|
soulmachine
2007-09-29
明白了,谢谢tomqyp
|