comp.lang.ada
 help / color / mirror / Atom feed
* constraint error with tagged type
@ 2003-02-23 12:31 Uri
  2003-02-23 13:39 ` David C. Hoos, Sr.
  0 siblings, 1 reply; 3+ messages in thread
From: Uri @ 2003-02-23 12:31 UTC (permalink / raw)


Hello,

I am trying to write a program that create polygons

I created a package called 'shapes':

	-- shapes.ads

	package shapes is
	   type point is record
	      X, Y : float;
	   end record;

	   type shape     is abstract tagged null record;
		... some function dealing with shape ...
	   type shape_ptr is access all shape'CLASS; 
   
	   type Polygon_Points is array (integer range <>) of point;
	   type polygon(Edges : integer) is new shape with record
	      vertex : Polygon_Points (1..Edges);
	   end record;
	   type polygon_ptr              is access all polygon'CLASS;
		... some function dealing with polygon ...
	   function  make_polygon(Point_Array : Polygon_Points) 
                                     return polygon_ptr;
	   procedure put_values  (poly : out polygon; 
                                  Pt_Array : Polygon_Points);
	private
	   	... some private function ...
	end shapes;

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

	-- shapes.adb 

	package body shapes is

		... some finctions ...

	   function  make_polygon(Point_Array : Polygon_Points) 
                                    return                            
                               polygon_ptr is
           -- create a polygon
	      This_Shape : polygon_ptr := new polygon(Point_Array'LENGTH);
	   begin
	      put_values(This_Shape.all, Point_Array);
	      return This_Shape;   
	   end make_polygon;
   
   	procedure put_values  (poly : out polygon; 
                               Pt_Array : Polygon_Points) is
	   -- put values in a polygon
	   begin
	      for I in Pt_Array'RANGE loop
	         poly.vertex(I) := Pt_Array(I);
	      end loop;
	   end put_values;

	end shapes;

I tried to create a polygon in my main program:

	-- main.adb

	with shapes;
	use  shapes;

	procedure main is
	   shape_array : array (1..4) of shape_ptr;
	begin
	    -- create a polygon
              ... blah, blah, blah ...                          
	   shape_array(3) := shape_ptr(make_polygon (((0.0, 0.0),
        	                                      (0.0, 3.0),
	                                              (4.0, 0.0),
	                                              (4.0, 3.0))));
              ... blah, blah, blah ...
	end main;

but when I ran it I got a CONTRAINT_ERROR, while using the debuger I
got something that looks like ____gant_malloc, but I don't understad
why the program can't create an array of points with only 4 members.

Please tell me what I did wrong.



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

* Re: constraint error with tagged type
  2003-02-23 12:31 constraint error with tagged type Uri
@ 2003-02-23 13:39 ` David C. Hoos, Sr.
  2003-02-24  8:58   ` Uri
  0 siblings, 1 reply; 3+ messages in thread
From: David C. Hoos, Sr. @ 2003-02-23 13:39 UTC (permalink / raw)
  To: comp.lang.ada mail to news gateway

The reason for the constraint error is quite simple.  Since
The index type for vertices is Integer, your call to
make_polygon in main creates an array with indices
Integer'First .. Integer'First + 3 -- i.e.,
-2147483648 ..-2147483645

Then, in put_values, you try to assign to poly.vertex (1 .. 4)
(i.e., the range of Pt_Array).

There are at least two ways to solve the problem.

The simplest would be to get rid of the procedure put_values
altogether, and in make_polygon simply say:
      This_Shape.Vertex := Point_Array;
This requires only that the lengths of the arrays be equal,
not that their indices coincide.

Another way is to simply assign indices 1 .. 4 in your
call to make_polygon, i.e.: 

   shape_array(3) := shape_ptr(make_polygon ((1 => (0.0, 0.0),
                                              2 => (0.0, 3.0),
                                              3 => (4.0, 0.0),
                                              4 => (4.0, 3.0))));

It would probably be better to use the type natural for Edges and
the vertex indices, as a polygon with a negative number of edges
probably has no meaning.

----- Original Message ----- 
From: "Uri" <dromy@isdn.net.il>
Newsgroups: comp.lang.ada
To: <comp.lang.ada@ada.eu.org>
Sent: February 23, 2003 6:31 AM
Subject: constraint error with tagged type


> Hello,
> 
> I am trying to write a program that create polygons
> 
> I created a package called 'shapes':
> 
> -- shapes.ads
> 
> package shapes is
>    type point is record
>       X, Y : float;
>    end record;
> 
>    type shape     is abstract tagged null record;
> ... some function dealing with shape ...
>    type shape_ptr is access all shape'CLASS; 
>    
>    type Polygon_Points is array (integer range <>) of point;
>    type polygon(Edges : integer) is new shape with record
>       vertex : Polygon_Points (1..Edges);
>    end record;
>    type polygon_ptr              is access all polygon'CLASS;
> ... some function dealing with polygon ...
>    function  make_polygon(Point_Array : Polygon_Points) 
>                                      return polygon_ptr;
>    procedure put_values  (poly : out polygon; 
>                                   Pt_Array : Polygon_Points);
> private
>    ... some private function ...
> end shapes;
> 
> --------------------------------------------------------------------------------
> 
> -- shapes.adb 
> 
> package body shapes is
> 
> ... some finctions ...
> 
>    function  make_polygon(Point_Array : Polygon_Points) 
>                                     return                            
>                                polygon_ptr is
>            -- create a polygon
>       This_Shape : polygon_ptr := new polygon(Point_Array'LENGTH);
>    begin
>       put_values(This_Shape.all, Point_Array);
>       return This_Shape;   
>    end make_polygon;
>    
>    procedure put_values  (poly : out polygon; 
>                                Pt_Array : Polygon_Points) is
>    -- put values in a polygon
>    begin
>       for I in Pt_Array'RANGE loop
>          poly.vertex(I) := Pt_Array(I);
>       end loop;
>    end put_values;
> 
> end shapes;
> 
> I tried to create a polygon in my main program:
> 
> -- main.adb
> 
> with shapes;
> use  shapes;
> 
> procedure main is
>    shape_array : array (1..4) of shape_ptr;
> begin
>     -- create a polygon
>               ... blah, blah, blah ...                          
>    shape_array(3) := shape_ptr(make_polygon (((0.0, 0.0),
>                                               (0.0, 3.0),
>                                               (4.0, 0.0),
>                                               (4.0, 3.0))));
>               ... blah, blah, blah ...
> end main;
> 
> but when I ran it I got a CONTRAINT_ERROR, while using the debuger I
> got something that looks like ____gant_malloc, but I don't understad
> why the program can't create an array of points with only 4 members.
> 
> Please tell me what I did wrong.
> _______________________________________________
> comp.lang.ada mailing list
> comp.lang.ada@ada.eu.org
> http://ada.eu.org/mailman/listinfo/comp.lang.ada
> 
> 




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

* Re: constraint error with tagged type
  2003-02-23 13:39 ` David C. Hoos, Sr.
@ 2003-02-24  8:58   ` Uri
  0 siblings, 0 replies; 3+ messages in thread
From: Uri @ 2003-02-24  8:58 UTC (permalink / raw)


Thanks, that really did the trick.



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

end of thread, other threads:[~2003-02-24  8:58 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-02-23 12:31 constraint error with tagged type Uri
2003-02-23 13:39 ` David C. Hoos, Sr.
2003-02-24  8:58   ` Uri

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