2014-11-18 4 views
3

저는 파스칼에서 간단한 세트로 작업하고 있습니다. 간단히 세트의 내용을 출력하고 싶습니다. 때마다 코드를 실행할 때마다 다음 오류 메시지가 나타납니다. 'project1.lpr (17,13) 오류 :이 형식의 변수를 읽거나 쓸 수 없습니다.'파스칼로 세트의 내용을 어떻게 출력합니까?

program Project1; 

{$mode objfpc}{$H+} 

uses 
    sysutils; 

type TFriends = (Anne,Bob,Claire,Derek,Edgar,Francy); 
type TFriendGroup = Set of TFriends; 

Var set1,set2,set3,set4:TFriendGroup; x:integer; 

begin 
set1:=[Anne,Bob,Claire]; 
set2:=[Claire,Derek]; 
set3:=[Derek,Edgar,Francy]; 
writeln(set1); 
readln; 
end. 

출력 세트에 대한 특별한 방법/기능이 있습니까 : 여기

내 코드?

감사

답변

3

그것을 위해 나간 유형 정보가 없기 때문에 직접 문자열로 세트를 표시 할 수 없습니다. 그렇게하려면 집합이 게시 된 클래스의 속성이어야합니다.

는 일단 클래스에 게시, 당신은 기능 SetToString()를 사용하여 문자열로 세트를 표시하는 단위 TypInfo를 사용할 수 있습니다. TypInfo은 모든 컴파일러 반영 사항을 수행하는 FPC 장치입니다.

당신이하려고 무엇을

짧은 작업 예 :

program Project1; 
{$mode objfpc}{$H+} 
uses 
    sysutils, typinfo; 

type 
    TFriends = (Anne,Bob,Claire,Derek,Edgar,Francy); 
    TFriendGroup = Set of TFriends; 

    TFoo = class 
    private 
    fFriends: TFriendGroup; 
    published 
    property Friends: TFriendGroup read fFriends write fFriends; 
    end; 

Var 
    Foo: TFoo; 
    FriendsAsString: string; 
    Infs: PTypeInfo; 

begin 
    Foo := TFoo.Create; 
    Foo.Friends := [Derek, Edgar, Francy]; 
    // 
    Infs := TypeInfo(Foo.Friends); 
    FriendsAsString := SetToString(Infs, LongInt(Foo.Friends), true); 
    // 
    Foo.Free; 
    writeln(FriendsAsString); 
    readln; 
end. 

이 프로그램의 출력 :

[Derek,Edgar,Francy]

더 이동하십시오 :

+0

도움을 주셔서 감사합니다. 나는 그것이 그렇게 간단하지 않다고 짐작했다. – user3396486

5

Free Pascal은 명시적인 typinfo 호출없이 enum의 write/writeln()을 허용합니다.

그래서

{$mode objfpc} // or Delphi, For..in needs Object Pascal dialect iirc. 
var Person :TFriends; 

for Person in Set1 do 
    writeln(Person); 

잘 작동합니다.

WriteStr을 사용하면이 값을 문자열에도 쓸 수 있습니다. (writestr는 write/writestr와 비슷하지만 문자열은 원래는 ISO/Mac 방언으로 구현되었습니다.)

관련 문제