From: Peter Hermann <ica2ph@csv.ica.uni-stuttgart.de>
Subject: Re: accessing record element (question to the pros)
Date: Fri, 24 Feb 2006 15:27:28 +0000 (UTC)
Date: 2006-02-24T15:27:28+00:00 [thread overview]
Message-ID: <dtn8l0$bqh$1@infosun2.rus.uni-stuttgart.de> (raw)
In-Reply-To: dtn78j$tdn$02$1@news.t-online.com
Norbert Caspari <nnc@gmx.li> wrote:
> I'm new to Ada programming and until now I wrote just some small pieces of
> code to try out new things and to educate myself. In this example I try to
> build a Union Record, where I can access a single Byte either as a small
> positive Integer value or as single Bits. I don't understand, why there is
> a compiler error in line 56 and why it is forbidden in this case to assign
> just a simple positive Integer value. If I comment this line out, the rest
> of the code will compile without errors. Please remember, this is just some
> experimental code with no special sense and no meaningful variable names.
>
> Could somebody please help me to correct my code, showing me how to access
how to assign ?
> a numeric value to the variable Yyy.
>
> Thank you very much for your help!
>
> Best regards, Norbert
>
> ----------------------------------------------------------
> GNAT 3.15p (20020523) Copyright 1992-2002 Free Software Foundation, Inc.
>
> Compiling: j:/source/uniontest4.adb (source file time stamp: 2006-02-24
> 14:43:50)
>
> 1. with Ada.Text_Io;
> 2. use Ada.Text_Io;
> 3.
> 4. procedure Uniontest4 is
> 5.
> 6. type Bit_T is
> 7. (Off,
> 8. On);
may be cumbersome to replace 0 by off etc
why not boolean?
> 9.
> 10. for Bit_T use (
> 11. Off => 0,
> 12. On => 1);
> 13.
> 14. type Bit8 is array (0 .. 7) of Bit_T;
> 15.
> 16. pragma Pack(Bit8);
> 17. for Bit8'Size use 8;
> 18.
> 19. type Uint8 is mod 2**8;
> 20.
> 21. for Uint8'Size use 8;
> 22.
> 23. type Form is
> 24. (Bytemode,
> 25. Bitmode);
> 26.
> 27. type Bfeld (Ftyp: Form) is
> 28. record
> 29. case Ftyp is
> 30. when Bitmode =>
> 31. Bit: Bit8;
> 32. when Bytemode =>
> 33. Byte : Uint8;
> 34. end case;
> 35. end record;
> 36.
> 37. pragma Pack(Bfeld);
> 38.
> 39. for Bfeld use record
> 40. at mod 1;
> 41. Bit at 0 range 0..7;
> 42. Byte at 0 range 0..7;
> 43. end record;
> 44.
> 45. type Bfeld_Bitmode is new Bfeld
> 46. (Bitmode);
> 47. type Bfeld_Bytemode is new Bfeld
> 48. (Bytemode);
with these declarations you have lost the advantage of the
discriminated record bfeld
> 49.
> 50. Xxx : Bfeld_Bitmode;
> 51. Yyy : Bfeld_Bytemode;
> 52.
> 53. begin
> 54.
> 55. Xxx.Bit(2) := On;
> 56. Yyy := 20;
yyy.byte := 20;
> |
> >>> expected type "Bfeld_Bytemode" defined at line 47
> >>> found type universal integer
>
> 57.
> 58. for K in 0 .. 7
> 59. loop
> 60. Put(Bit_T'Image(Xxx.Bit(K)));
> 61. end loop;
> 62. New_Line;
> 63.
> 64. end Uniontest4;
> 65.
>
> 65 lines: 2 errors
>
--
--Peter.Hermann@ihr.uni-stuttgart.de (+49)0711-685-87244
--Nobelstr.19 Raum 0.030, D-70569 Stuttgart IHR Hoechstleistungsrechnen
--http://www.ihr.uni-stuttgart.de/ Fax 0711-89238279
next prev parent reply other threads:[~2006-02-24 15:27 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-02-24 15:03 accessing record element (question to the pros) Norbert Caspari
2006-02-24 15:27 ` Peter Hermann [this message]
2006-02-24 17:07 ` Norbert Caspari
2006-02-24 17:08 ` Alex R. Mosteo
2006-02-24 18:02 ` Jeffrey R. Carter
2006-02-24 19:04 ` Simon Wright
2006-02-24 21:46 ` Randy Brukardt
2006-02-26 11:49 ` many thanks to all the people who give me useful hints! Norbert Caspari
2006-02-28 0:33 ` Randy Brukardt
2006-02-24 18:32 ` accessing record element (question to the pros) Martin Krischik
2006-02-28 20:02 ` Matthew Heaney
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox