2013-10-04 71 views
1

我已这应该分割一个字符串转换为字符串数组(我使用Geany IDE和FPC编译器)下面的函数:类型标识符预期FPC编译器错误

function Split(const str: string; const separator: string): array of string; 
var 
i, n: integer; 
strline, strfield: string; 
    begin 
n:= Occurs(str, separator); 
SetLength(Result, n + 1); 
i := 0; 
strline:= str; 
repeat 
if Pos(separator, strline) > 0 then 
begin 
    strfield:= Copy(strline, 1, Pos(separator, strline) - 1); 
    strline:= Copy(strline, Pos(separator, strline) + 1, 
Length(strline) - pos(separator,strline)); 
end 
else 
begin 
    strfield:= strline; 
    strline:= ''; 
end; 
Result[i]:= strfield; 
Inc(i); 
until strline= ''; 
if Result[High(Result)] = '' then SetLength(Result, Length(Result) -1); 
end; 

编译器报告一个错误:

calc.pas(24,61) Error: Type identifier expected 
calc.pas(24,61) Fatal: Syntax error, ";" expected but "ARRAY" found 

据我所看到的语法是正确的,这里有什么问题?

+1

编译器告诉你,你不能返回一个无类型的动态数组。申报f.i. 'type TStringArray = string of array;'你可以从函数返回'TStringArray'。 –

+0

啊thx,发布这个答案,我会接受它。 Pascal看起来很奇怪,经过多年的java>< – Droidman

+0

完成,欢迎您。但请注意你的问题一段时间,因为我不确定这种行为是否取决于某种“模式”。如果是,有人可能会提供一个扩展答案。 –

回答

3

编译器告诉你,你不能返回一个无类型的动态数组。你可以声明f.i.

type TStringArray = array of string; 

并且您可以从该函数返回一个TStringArray。请注意,声明为TStringArray的变量将与类似声明但类型不同的数组不兼容,例如type TOtherStringArray = array of string

相关问题