comp.lang.ada
 help / color / mirror / Atom feed
* Please help!  Aggregate Object - dynamically allocating the objects inside, doesnt work
@ 1999-01-09  0:00 Craig Garrett
  1999-01-09  0:00 ` David C. Hoos, Sr.
  0 siblings, 1 reply; 3+ messages in thread
From: Craig Garrett @ 1999-01-09  0:00 UTC (permalink / raw)


I need help.  I am probably doing something stupid, but here is my
situation:

I have an aggregate object (Ada95) called Time_Manager.  Its a tagged
record, and two of its fields are pointers (access types) to different
classes.  In a procedure I am calling start that is in the Time_Manager
package, I try to use the new operator to dynamically allocate these fields
and get actual objects in there, not just pointers.  Pretty standard right?
 Its just aggregate objects - thats all.  Anyway, the compiler is choking
on it, where I call the new operator.

Here is the code:


package sim_clock is
.....
    type object is Tagged
        record
   	Current_Time      : long_float := 0.0;
	Last_Time         : long_float := 0.0; 
   	Time_Since_Last   : long_float := 0.0; 
	Last_Time_of_Day    : Ada.Calendar.Time; 
	Current_Time_of_Day : Ada.Calendar.Time; 
        end record;
   type object_pointer is access object;
end sim_clock;


package wall_clock is
....
    type object is Tagged
        record
	Current_Time,
   	Last_Time         : Ada.Calendar.Time := Ada.Calendar.Clock;
   	Time_Since_Last   : Duration := 0.0;
         end record;
    type object_pointer is access object;
end wall_clock;


then the aggregate object, Time_Manager is:

package time_manager is
    type object is Tagged
        record
   	Wall_Time         : wall_clock.object_pointer;
   	Sim_Time          : sim_clock.object_pointer;
        end record;
    type object_pointer is access object;
end time_manager;

Ok, then I have a Start procedure in time_manager :

    procedure Start(this_time_manager : in out object_pointer) is
    begin
        this_time_manager.Wall_Time := new wall_clock.object;
        this_time_manager.Sim_Time  := new sim_clock.object;
    end Start;

The compiler is choking on the two "new" lines, this is the error:

time_manager.adb: Error: line 15 col 40 LRM:5.2(4), Type mismatch in
assignment statement, continuing 

time_manager.adb: Error: line 16 col 40 LRM:5.2(4), Type mismatch in
assignment statement, continuing 

Now, it has always been my understanding that dynamic allocation goes like
this: 
   You create an access type to some type (integer, record, whatever) and
then declare a variable of that access type.
   Then, you call the new operator like this:
       <access variable>  :=  new <TYPE>;

so if I have an access type to an integer called int_pointer, then I could:
  my_int_pointer : int_pointerr;
begin
  my_int_pointer := new INTEGER;

and that would work right?  That is what I read in the books, but it doesnt
seem to work with my aggregate objects.
I am at a total loss, I can provide all the code if you want, please help!
   - Craig





^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Please help!  Aggregate Object - dynamically allocating the objects inside, doesnt work
  1999-01-09  0:00 Please help! Aggregate Object - dynamically allocating the objects inside, doesnt work Craig Garrett
@ 1999-01-09  0:00 ` David C. Hoos, Sr.
  1999-01-09  0:00   ` Craig Garrett
  0 siblings, 1 reply; 3+ messages in thread
From: David C. Hoos, Sr. @ 1999-01-09  0:00 UTC (permalink / raw)



Craig Garrett wrote in message <01be3b99$6fe27620$5104fbd1@longslide>...
>I need help.  I am probably doing something stupid, but here is my
>situation:
>
>I have an aggregate object (Ada95) called Time_Manager.  Its a tagged
>record, and two of its fields are pointers (access types) to different
>classes.  In a procedure I am calling start that is in the Time_Manager
>package, I try to use the new operator to dynamically allocate these fields
>and get actual objects in there, not just pointers.  Pretty standard right?
> Its just aggregate objects - thats all.  Anyway, the compiler is choking
>on it, where I call the new operator.
>
What compiler are you using?

The following code compiles just fine with GNAT.


David C. Hoos, Sr.

-- begin source code --
with Ada.Calendar;
package Sim_Clock is
   type Object is tagged
      record
         Current_Time      : Long_Float := 0.0;
         Last_Time         : Long_Float := 0.0;
         Time_Since_Last   : Long_Float := 0.0;
         Last_Time_Of_Day    : Ada.Calendar.Time;
         Current_Time_Of_Day : Ada.Calendar.Time;
      end record;
   type Object_Pointer is access Object;
end Sim_Clock;

with Ada.Calendar;
package Wall_Clock is
   type Object is tagged
      record
         Current_Time,
           Last_Time         : Ada.Calendar.Time := Ada.Calendar.Clock;
         Time_Since_Last   : Duration := 0.0;
      end record;
   type Object_Pointer is access Object;
end Wall_Clock;

with Sim_Clock;
with Wall_Clock;
package Time_Manager is
   type Object is tagged
      record
         Wall_Time         : Wall_Clock.Object_Pointer;
         Sim_Time          : Sim_Clock.Object_Pointer;
      end record;
   type Object_Pointer is access Object;
   procedure Start(This_Time_Manager : in out Object_Pointer);
end Time_Manager;

package body Time_Manager is
   procedure Start(This_Time_Manager : in out Object_Pointer) is
   begin
      This_Time_Manager.Wall_Time := new Wall_Clock.Object;
      This_Time_Manager.Sim_Time  := new Sim_Clock.Object;
   end Start;
end Time_Manager;
-- end source code --








^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Please help!  Aggregate Object - dynamically allocating the objects inside, doesnt work
  1999-01-09  0:00 ` David C. Hoos, Sr.
@ 1999-01-09  0:00   ` Craig Garrett
  0 siblings, 0 replies; 3+ messages in thread
From: Craig Garrett @ 1999-01-09  0:00 UTC (permalink / raw)


actually, I was able to get it to work, the problem was that I had the
object_ptr types as private.
(I should have included that fact, sorry)




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~1999-01-09  0:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-01-09  0:00 Please help! Aggregate Object - dynamically allocating the objects inside, doesnt work Craig Garrett
1999-01-09  0:00 ` David C. Hoos, Sr.
1999-01-09  0:00   ` Craig Garrett

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox