我目前遇到了这些错误的问题,似乎无法通过它们,我已经将我的错误以及我的代码附加在下面,谢谢。
错误:
免费Pascal编译器版本2.6.4 2014/02/26,适用于i386版权所有(c) 1993-2014目标OS: Darwin for i386编译BasicReadWrite.pas BasicReadWrite.pas(22,30)错误:不兼容的类型:获取"personArray“预期的"LongInt”BasicReadWrite.pas(25,8)错误:没有可用的默认属性BasicReadWrite.pas(25,8)致命:语法错误,";“期望但"[”发现致命:编译中止错误: /usr/local/bin/ppc386返回错误退出代码(如果未指定要编译的源文件,则正常)
program BasicReadWrite;
type
Person = record
name: String;
age: String; // Should this be an integer? Why/Why not?
end;
personArray = array of Person;
procedure WriteLinesToFile(var myFile: TextFile; const pe: Person);
begin
WriteLn(myFile, pe.age);
WriteLn(myFile, pe.name);
end;
procedure PrintRecords(const ArrayOfPersons: personArray; count: Integer);
var
p: Person;
begin
setLength(p, ArrayOfPersons);
for count:= 0 to high(ArrayOfPersons) do
begin
p[count] := WriteLinesToFile();
end;
end;
procedure ReadLinesFromFile(var myFile: TextFile);
var
p: Person;
number: Integer;
ArrayOfPersons: personArray;
begin
for number:= 0 to 20 do
begin
PrintRecords([number]);
end;
end;
procedure Main();
var
myFile: TextFile;
begin
AssignFile(myFile, 'mytestfile.dat');
ReWrite(myFile); // Use ReWrite to open a file for writing
WriteLinesToFile(myFile);
Close(myFile); // We need to close the file and re-open it, as Pascal
// will not let you Read and write from a file at the same time.
AssignFile(myFile, 'mytestfile.dat');
Reset(myFile); // Open the file for reading.
ReadLinesFromFile(myFile);
Close(myFile);
end;
begin
Main();
end.
发布于 2018-05-29 16:47:57
通常,第一个错误是首先要关注的错误。下面的错误可能只是第一个错误的后果。我将帮助您从第一个错误开始,但将其余的留给您来解决。你可能想和你的导师讨论一下这些错误。
所以,首先关注的是
BasicReadWrite.pas(22,30)错误:类型不兼容:获取"personArray“应为"LongInt”
第22行在
procedure PrintRecords(const ArrayOfPersons: personArray; count: Integer);
var
p: Person;
begin
setLength(p, ArrayOfPersons); // line 22
这一行是错误的,因为:
p
是Person
类型的记录。您不能将record
.SetLength()
必须是integer
。ArrayOfPersons
不是整数。我看不出有任何理由在该过程中设置任何内容的长度。
发布于 2018-05-30 09:29:25
以防你需要一些解释,当你声明的时候:
personArray = array of Person;
这意味着personArray是一个动态数组。首先,在使用之前,您需要指定此动态数组的长度,例如:
setlength(personArray,20);
其中20是要存储在personArray中的索引量(不要忘记第一个索引是0!)。例如:
personArray[0].name:= 'John';
年龄: String;//是否为整数?为什么/为什么不?
当然是,年龄应该是一个整数。您可以在字符串中存储年龄,但整数占用的内存较少,并且可以对整数使用数学运算(如果需要添加或减去年龄怎么办?你不能用字符串做数学运算)。仅当需要存储字母数字数据时,才应使用字符串。
祝好运!
https://stackoverflow.com/questions/50577442
复制相似问题