comp.lang.ada
 help / color / mirror / Atom feed
* oo problem help please
@ 2003-12-08 19:49 shoko
  2003-12-08 23:19 ` Marius Amado Alves
  2003-12-09 16:42 ` Martin Krischik
  0 siblings, 2 replies; 4+ messages in thread
From: shoko @ 2003-12-08 19:49 UTC (permalink / raw)


what is wrong with the following code:

----car.ads----
package car is
  --car class --
  type car is tagged private; 
  type car_ptr is access all car'class;
  
  --car methods --
  procedure set_name(name:String;this:in out car);
  function  get_name(this:car) return string;
  
 private
     type car is tagged 
     record
         name:String(1..256);
     end record;
  
   
end car;
---car.adb---
package body car is
  procedure set_name(name:String;This:in out car)is
  begin 
     this.name:=name;
  end set_name;
  
  function  get_name(this:car) return string is
  begin 
     return this.name; 
  end get_name;  
end car;
----transport.ads-----
with car;

package transport is
   type transport_rec is new car.car with private;
   type transport_Ptr is access all transport_rec'Class;

   function init_transport(maximum_speed:positive) return transport_Ptr;
   
   private
     type transport_rec is new  car.car with null record;
         
end transport;

-----transport.adb----
package body transport is
   function init_transport(maximum_speed:positive)return transport_Ptr is
        This: transport_Ptr;
   begin
      This:= new transport_Rec;
         
      return this;
   end init_transport;
  

end transport;

----------bus.ads---
with transport;

package bus is
   type bus is new transport.transport_rec with private ;
   type bus_Ptr is access all bus'Class;
   function initbus(name:string) return bus_Ptr;
   private
      type bus is new transport.transport_rec with null record;
end bus;    

---------bus.adb--------
package body bus is
   function initbus(name:String) return bus_Ptr is
       This: bus_ptr;
   begin
      This := new bus;
      set_name(name,this.all);   <--  CONSTRAINT ERROR
      
      return this;
   end initbus;

end bus;
------------------------------
and finally

--------main.adb--------------

with car;use car;
with bus;use bus;


procedure main is
   cars:array(1..2) of car_ptr;
   new_bus:bus_ptr;
   
  
begin
 
     cars(1) := car_ptr(initbus("mercedes"));
  
end main;


the problem is that i get constraint error when the code is executed
why???



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

* Re: oo problem help please
  2003-12-08 19:49 oo problem help please shoko
@ 2003-12-08 23:19 ` Marius Amado Alves
  2003-12-09 16:45   ` Martin Krischik
  2003-12-09 16:42 ` Martin Krischik
  1 sibling, 1 reply; 4+ messages in thread
From: Marius Amado Alves @ 2003-12-08 23:19 UTC (permalink / raw)
  To: comp.lang.ada

On Mon, 2003-12-08 at 19:49, shoko wrote:
> what is wrong with the following code:
> ...
>      type car is tagged 
>      record
>          name:String(1..256);
>      end record;
> ...
>   procedure set_name(name:String;This:in out car)is
>   begin 
>      this.name:=name;

My guess is this last line.

> ... the problem is that i get constraint error when the code is executed
> why???

Ditto.

Possible fixes:

- pad (or trim) to 256 before assigning;

- make sure the target is initialized to spaces (say), and that the
source is no longer than the target, and then write This.Name (1 ..
Target'Length) := Name;

- redefine the component to be a pointer to string, then write This.Name
:= new String' (Name);





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

* Re: oo problem help please
  2003-12-08 19:49 oo problem help please shoko
  2003-12-08 23:19 ` Marius Amado Alves
@ 2003-12-09 16:42 ` Martin Krischik
  1 sibling, 0 replies; 4+ messages in thread
From: Martin Krischik @ 2003-12-09 16:42 UTC (permalink / raw)


shoko wrote:

> what is wrong with the following code:

Wrong choices

> ----car.ads----
> package car is
>   --car class --
>   type car is tagged private;
>   type car_ptr is access all car'class;
>   
>   --car methods --
>   procedure set_name(name:String;this:in out car);
>   function  get_name(this:car) return string;
>   
>  private
>      type car is tagged
>      record
>          name:String(1..256);
>      end record;
>   

I don't belive that every name should be 256 characters long.

> begin
>ᅵᅵᅵᅵᅵcars(1)ᅵ:=ᅵcar_ptr(initbus("mercedes"));
ᅵᅵ
Yes "mercedes" is not 256 Characters. You should take a look at

http://www.adaic.org/standards/95lrm/html/RM-A-4-4.html
http://www.adaic.org/standards/95lrm/html/RM-A-4-5.html

Fact is that you got the wrong type of string. If there is a max lenght
(like you want to store your vechicles in a database) then use
Ada.Strings.Bounded if not use Ada.Strings.Unbounded.

Or if the name is constant use a discriminant:

type car (
    Name_Lenght : Positive)
is tagged record
    name : String(1 .. Name_Lenght);
end record;

With Regards

Martin

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




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

* Re: oo problem help please
  2003-12-08 23:19 ` Marius Amado Alves
@ 2003-12-09 16:45   ` Martin Krischik
  0 siblings, 0 replies; 4+ messages in thread
From: Martin Krischik @ 2003-12-09 16:45 UTC (permalink / raw)


Marius Amado Alves wrote:

> - redefine the component to be a pointer to string, then write This.Name
> := new String' (Name);

There are better ways then the use Pointers. Beginners are better of using
Ada.Strings.Unbounded (http://www.adaic.org/standards/95lrm/html
RM-A-4-5.html) and think about the advanced techniques laster.

I don't think he is planning for embedded software where
Ada.Strings.Unbounded might be a problem.

With Regards

Martin

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




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

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

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-12-08 19:49 oo problem help please shoko
2003-12-08 23:19 ` Marius Amado Alves
2003-12-09 16:45   ` Martin Krischik
2003-12-09 16:42 ` Martin Krischik

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