comp.lang.ada
 help / color / mirror / Atom feed
* please help - access and inheritence
@ 2003-12-07  0:47 Ratson Janiv
  2003-12-07  7:14 ` tmoran
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Ratson Janiv @ 2003-12-07  0:47 UTC (permalink / raw)


type Vehicleptr is access Jr.Vehicle.Vehicle'Class;

type Vehiclesarr is array (1..Maxvehilces) of Vehicleptr;

Vp1,Vp2,Vp3,Vp4,Vp5: aliased Jr.Vehicle.Private_Car.Vehicle_Private;

Arr(1) := Vehicleptr(Vp1'access); -- failed, why? please ...



regards,

janiv ratson.







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

* Re: please help - access and inheritence
  2003-12-07  0:47 please help - access and inheritence Ratson Janiv
@ 2003-12-07  7:14 ` tmoran
  2003-12-07 18:44 ` Jeffrey Carter
  2003-12-08 18:50 ` Martin Krischik
  2 siblings, 0 replies; 10+ messages in thread
From: tmoran @ 2003-12-07  7:14 UTC (permalink / raw)


> Arr(1) := Vehicleptr(Vp1'access); -- failed, why? please ...
"failed"? As in "made a loud noise and a flash and the computer went
dead and there was the smell of smoke in the air"?  Or perhaps
"failed" means "The compiler saw an error and gave a message
explaining the problem".  In that case, what did the compiler's
message say?  (The code fragment you posted was too incomplete
to submit to my Ada compiler to see what it would say.)



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

* Re: please help - access and inheritence
  2003-12-07  0:47 please help - access and inheritence Ratson Janiv
  2003-12-07  7:14 ` tmoran
@ 2003-12-07 18:44 ` Jeffrey Carter
  2003-12-08  4:46   ` Mr. J.
  2003-12-08 18:50 ` Martin Krischik
  2 siblings, 1 reply; 10+ messages in thread
From: Jeffrey Carter @ 2003-12-07 18:44 UTC (permalink / raw)


Ratson Janiv wrote:
> type Vehicleptr is access Jr.Vehicle.Vehicle'Class;
> 
> type Vehiclesarr is array (1..Maxvehilces) of Vehicleptr;
> 
> Vp1,Vp2,Vp3,Vp4,Vp5: aliased Jr.Vehicle.Private_Car.Vehicle_Private;
> 
> Arr(1) := Vehicleptr(Vp1'access); -- failed, why? please ...

You could be a bit more clear about what the problem is. Does "failed" 
mean a run-time problem or a compiler error? In either case, more 
information about the "failure" would be helpful.

I note that Vehicleptr is not a general access type (access all 
<subtype>), but only allows access to values allocated on the heap 
(access <subtype>). Since Vp1'access does not designate a value 
allocated on the heap, it cannot be assigned to an object of type 
Vehicleptr. So if Arr (1) is a Vehicleptr [which seems likely, since 
you're trying to convert Vp1'access to Vehicleptr before assigning it to 
Arr (1)], the compiler should dissallow the assignment.

If you define Vehicleptr as access all ..., and Arr is declared as
Vehiclesarr, then you can write

Arr (1) := Vp1'access;

I guess saving the time to type an underline is more important than 
having code that is easy to read by using names such as Vehicle_Ptr.

-- 
Jeff Carter
"Every sperm is sacred."
Monty Python's the Meaning of Life
55




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

* Re: please help - access and inheritence
  2003-12-07 18:44 ` Jeffrey Carter
@ 2003-12-08  4:46   ` Mr. J.
  2003-12-08 18:07     ` Jeffrey Carter
  2003-12-08 19:17     ` Martin Krischik
  0 siblings, 2 replies; 10+ messages in thread
From: Mr. J. @ 2003-12-08  4:46 UTC (permalink / raw)


10x,
It works now.

now, the next problem is as follow:

The Code:
===========================================================================
procedure Q1 is

   Maxvehilces : constant := 10;    -- arbitrary
            
   type Vehicleptr is access all Jr.Vehicle.Vehicle'Class;
   type Vehiclesarr is array (1..Maxvehilces) of Vehicleptr;
   
   Arr: Vehiclesarr;
   
   Vp1,Vp2,Vp3,Vp4,Vp5: aliased
Jr.Vehicle.Private_Car.Vehicle_Private;
   Vl6,Vl7,Vl8 : aliased Jr.Vehicle.Load.Vehicle_Load;
   Vp9,Vp10 : aliased Jr.Vehicle.Passengers.Vehicle_Passengers;
   
   Tmp :Vehicleptr;
   Speed: Natural;
            
begin

Daily_Max_Speed(Vp1,65);
      Daily_Max_Speed(Vp2,101);
      Daily_Max_Speed(Vp3,92);
      Daily_Max_Speed(Vp4,113);
      Daily_Max_Speed(Vp5,76);
      Daily_Max_Speed(Vl6,56);
      Daily_Max_Speed(Vl7,110);
      Daily_Max_Speed(Vl8,70);
      Daily_Max_Speed(Vp9,81);
      Daily_Max_Speed(Vp10,67);
            
      Arr(1) := Vp1'access;
      Arr(2) := Vp2'access;
      Arr(3) := Vp3'access;
      Arr(4) := Vp4'access;
      Arr(5) := Vp5'access;
      Arr(6) := Vl6'access;
      Arr(7) := Vl7'access;
      Arr(8) := Vl8'access;
      Arr(9) := Vp9'access;
      Arr(10) := Vp10'access;
            
      for I in Arr'range loop
         if(Has_Exceeded(Arr(2).all)) then
                                    
            Put_Line("Exceeded");
                  
         end if;
                       
      end loop;
      
end Q1;
===========================================================================

Has_Exceeded is a virtual function, the base class:
===========================================================================
package Jr.Vehicle is
   
   type Vehicle is abstract tagged private;
   Allowmaxspeed : Natural := 100;
      
   procedure Daily_Max_Speed(V: in out Vehicle; Speed: Natural);
   function  Daily_Max_Speed(V:Vehicle) return Natural;
   function  Has_Exceeded(V:Vehicle) return Boolean;
   function  Treatment_Date(V:Vehicle;LastTreatment:Ada.Calendar.Time)
return Ada.Calendar.Time;
   
   Illegalspeed: exception;   --when daily max speed exceeded possible
max speed
   
   private
      
      type Vehicle is new Controlled with record
         
         Possiblemaxspeed : Natural;  
         Dailymaxspeed : Natural := 0;
                  
      end record;
      
      procedure Initialize(V:in out Vehicle;Possiblemaxspeed:Natural);
       
end Jr.Vehicle;
===========================================================================




The Compiler Errors:

---------------------------------------------------------------------------
q1.adb:64:13: no candidate interpretations match the actuals:
q1.adb:64:33: expected private type "Vehicle_Private" defined at
jr-vehicle-private_car.ads:5
q1.adb:64:33: found type "Vehicle'Class" defined at jr-vehicle.ads:7
q1.adb:64:33:   ==> in call to "Has_Exceeded" at
jr-vehicle-private_car.ads:5(inherited)
q1.adb:64:33:   ==> in call to "Has_Exceeded" at
jr-vehicle-passengers.ads:7
q1.adb:64:33:   ==> in call to "Has_Exceeded" at jr-vehicle-load.ads:7
End of compilation
----------------------------------------------------------------------------


10x for your help.


Jeffrey Carter <spam@spam.com> wrote in message news:<lyKAb.4224$rP6.87@newsread2.news.pas.earthlink.net>...
> Ratson Janiv wrote:
> > type Vehicleptr is access Jr.Vehicle.Vehicle'Class;
> > 
> > type Vehiclesarr is array (1..Maxvehilces) of Vehicleptr;
> > 
> > Vp1,Vp2,Vp3,Vp4,Vp5: aliased Jr.Vehicle.Private_Car.Vehicle_Private;
> > 
> > Arr(1) := Vehicleptr(Vp1'access); -- failed, why? please ...
> 
> You could be a bit more clear about what the problem is. Does "failed" 
> mean a run-time problem or a compiler error? In either case, more 
> information about the "failure" would be helpful.
> 
> I note that Vehicleptr is not a general access type (access all 
> <subtype>), but only allows access to values allocated on the heap 
> (access <subtype>). Since Vp1'access does not designate a value 
> allocated on the heap, it cannot be assigned to an object of type 
> Vehicleptr. So if Arr (1) is a Vehicleptr [which seems likely, since 
> you're trying to convert Vp1'access to Vehicleptr before assigning it to 
> Arr (1)], the compiler should dissallow the assignment.
> 
> If you define Vehicleptr as access all ..., and Arr is declared as
> Vehiclesarr, then you can write
> 
> Arr (1) := Vp1'access;
> 
> I guess saving the time to type an underline is more important than 
> having code that is easy to read by using names such as Vehicle_Ptr.



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

* Re: please help - access and inheritence
  2003-12-08  4:46   ` Mr. J.
@ 2003-12-08 18:07     ` Jeffrey Carter
  2003-12-08 19:05       ` Martin Krischik
  2003-12-08 19:17     ` Martin Krischik
  1 sibling, 1 reply; 10+ messages in thread
From: Jeffrey Carter @ 2003-12-08 18:07 UTC (permalink / raw)


Mr. J. wrote:
>          if(Has_Exceeded(Arr(2).all)) then

What happens if you change this to Arr (I), which I'm sure you must have 
intended?

> Has_Exceeded is a virtual function, the base class:

Ada doesn't have virtual functions.

-- 
Jeff Carter
"If I could find a sheriff who so offends the citizens of Rock
Ridge that his very appearance would drive them out of town ...
but where would I find such a man? Why am I asking you?"
Blazing Saddles
37




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

* Re: please help - access and inheritence
  2003-12-07  0:47 please help - access and inheritence Ratson Janiv
  2003-12-07  7:14 ` tmoran
  2003-12-07 18:44 ` Jeffrey Carter
@ 2003-12-08 18:50 ` Martin Krischik
  2003-12-09  5:58   ` Mr. J.
  2 siblings, 1 reply; 10+ messages in thread
From: Martin Krischik @ 2003-12-08 18:50 UTC (permalink / raw)


Ratson Janiv wrote:

> type Vehicleptr is access Jr.Vehicle.Vehicle'Class;

An other poster allready pointed out that you need to use access all. 

> type Vehiclesarr is array (1..Maxvehilces) of Vehicleptr;
> 
> Vp1,Vp2,Vp3,Vp4,Vp5: aliased Jr.Vehicle.Private_Car.Vehicle_Private;
> 
> Arr(1) := Vehicleptr(Vp1'access); -- failed, why? please ...

But maybee you would be better off using some container library i.E. the
booch components:

http://www.pushface.org/components/bc

and since you are trying to store 

Vehicle'Class you might also want to look at the indefinte extension at:

http://adacl.sourceforge.net

Examples:

http://adacl.sourceforge.net/html
______Include__AdaCL-SAR-Filter-Container__ads.htm

http://adacl.sourceforge.net/html/sarDo-CommandLine__adb.htm#176_15

The interesting Part: You won't need any access any more. 

With Regards

Martin

-- 
mailto://krischik@users.sourceforge.net
http://adacl.sourceforge.net




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

* Re: please help - access and inheritence
  2003-12-08 18:07     ` Jeffrey Carter
@ 2003-12-08 19:05       ` Martin Krischik
  0 siblings, 0 replies; 10+ messages in thread
From: Martin Krischik @ 2003-12-08 19:05 UTC (permalink / raw)


Jeffrey Carter wrote:

> Mr. J. wrote:
>>          if(Has_Exceeded(Arr(2).all)) then
> 
> What happens if you change this to Arr (I), which I'm sure you must have
> intended?
> 
>> Has_Exceeded is a virtual function, the base class:
> 
> Ada doesn't have virtual functions.

Be fair to the beginer. Ada does have dispaching.

With Regards

Martin

-- 
mailto://krischik@users.sourceforge.net
http://adacl.sourceforge.net




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

* Re: please help - access and inheritence
  2003-12-08  4:46   ` Mr. J.
  2003-12-08 18:07     ` Jeffrey Carter
@ 2003-12-08 19:17     ` Martin Krischik
  1 sibling, 0 replies; 10+ messages in thread
From: Martin Krischik @ 2003-12-08 19:17 UTC (permalink / raw)


Mr. J. wrote:

I show you the verbose way of typing it:


>       for I in Arr'range loop
>          if(Has_Exceeded(Arr(2).all)) then

Process : declare
    Current_Vehicle : Vehicle'Class renames Arr(I).all;
begin
          if(Has_Exceeded(Current_Vehicle)) then
                                     
>             Put_Line("Exceeded");
>                   
>          end if;

end Process;
                        
>       end loop;
>       
> end Q1;

In Ada dispaching is not done by writing "virtual" when declaring the
function but by using 'Class when calling the function.

With Regards

Martin

-- 
mailto://krischik@users.sourceforge.net
http://adacl.sourceforge.net




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

* Re: please help - access and inheritence
  2003-12-08 18:50 ` Martin Krischik
@ 2003-12-09  5:58   ` Mr. J.
  2003-12-09 16:55     ` Martin Krischik
  0 siblings, 1 reply; 10+ messages in thread
From: Mr. J. @ 2003-12-09  5:58 UTC (permalink / raw)


10x, but it still doesn't compiled :
==========================================
for I in Arr'range loop         
         Process :            
            declare                             
            Current_Vehicle : Jr.Vehicle.Vehicle'Class renames
Arr(I).all;
         begin                            
            if(Has_Exceeded(Current_Vehicle)) then               
               Put_Line("Exceeded");               
            end if;            
         end Process;         
      end loop;
===============================================
compile error : 
q1.adb:79:16: no candidate interpretations match the actuals:
q1.adb:79:29: expected private type "Vehicle_Private" defined at
jr-vehicle-private_car.ads:5
q1.adb:79:29: found type "Vehicle'Class" defined at jr-vehicle.ads:7
q1.adb:79:29:   ==> in call to "Has_Exceeded" at
jr-vehicle-private_car.ads:5(inherited)
q1.adb:79:29:   ==> in call to "Has_Exceeded" at
jr-vehicle-passengers.ads:7
q1.adb:79:29:   ==> in call to "Has_Exceeded" at jr-vehicle-load.ads:7

-------------------------------------------------------------------------


Martin Krischik <krischik@users.sourceforge.net> wrote in message news:<1339724.UG9tll5RLS@linux1.krischik.com>...
> Ratson Janiv wrote:
> 
> > type Vehicleptr is access Jr.Vehicle.Vehicle'Class;
> 
> An other poster allready pointed out that you need to use access all. 
> 
> > type Vehiclesarr is array (1..Maxvehilces) of Vehicleptr;
> > 
> > Vp1,Vp2,Vp3,Vp4,Vp5: aliased Jr.Vehicle.Private_Car.Vehicle_Private;
> > 
> > Arr(1) := Vehicleptr(Vp1'access); -- failed, why? please ...
> 
> But maybee you would be better off using some container library i.E. the
> booch components:
> 
> http://www.pushface.org/components/bc
> 
> and since you are trying to store 
> 
> Vehicle'Class you might also want to look at the indefinte extension at:
> 
> http://adacl.sourceforge.net
> 
> Examples:
> 
> http://adacl.sourceforge.net/html
> ______Include__AdaCL-SAR-Filter-Container__ads.htm
> 
> http://adacl.sourceforge.net/html/sarDo-CommandLine__adb.htm#176_15
> 
> The interesting Part: You won't need any access any more. 
> 
> With Regards
> 
> Martin



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

* Re: please help - access and inheritence
  2003-12-09  5:58   ` Mr. J.
@ 2003-12-09 16:55     ` Martin Krischik
  0 siblings, 0 replies; 10+ messages in thread
From: Martin Krischik @ 2003-12-09 16:55 UTC (permalink / raw)


Mr. J. wrote:

I forgot the cast.

> 10x, but it still doesn't compiled :
> ==========================================
> for I in Arr'range loop
>          Process :
>             declare
>             Current_Vehicle : Jr.Vehicle.Vehicle'Class renames
> Arr(I).all;

Current_Vehicle : Jr.Vehicle.Vehicle'Class renames Jr.Vehicle.Vehicle'Class
(Arr(I).all);


>          begin
>             if(Has_Exceeded(Current_Vehicle)) then
>                Put_Line("Exceeded");
>             end if;
>          end Process;
>       end loop;

Might you, get yourself a good Ada Book because you have problems with basic
syntax problems while trying to do some advanced programing.

Or look at some working programs - just for enlightenment:

http://adacl.sourceforge.net/html/sarOAda-CommandLine__adb.htm#133_15

With Regards

Martin

-- 
mailto://krischik@users.sourceforge.net
http://adacl.sourceforge.net




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

end of thread, other threads:[~2003-12-09 16:55 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-12-07  0:47 please help - access and inheritence Ratson Janiv
2003-12-07  7:14 ` tmoran
2003-12-07 18:44 ` Jeffrey Carter
2003-12-08  4:46   ` Mr. J.
2003-12-08 18:07     ` Jeffrey Carter
2003-12-08 19:05       ` Martin Krischik
2003-12-08 19:17     ` Martin Krischik
2003-12-08 18:50 ` Martin Krischik
2003-12-09  5:58   ` Mr. J.
2003-12-09 16:55     ` Martin Krischik

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