adagnat

What caused this Ada compilation error "ambiguous character literal"?


I have this Ada code.

with Ada.Text_IO; 
use Ada.Text_IO;

procedure for_Loop is
begin
    for Counter in 'A'..'Z' loop
        Put(Counter);   
    end loop;
    New_Line;
end for_Loop;

The Ada compiler (gnatmake) outputs these error message.

gcc -c for_loop.adb
for_loop.adb:6:24: ambiguous character literal
for_loop.adb:6:24: possible interpretation: Character
for_loop.adb:6:24: possible interpretation: Wide_Character
for_loop.adb:6:24: possible interpretation: Wide_Wide_Character
gnatmake: "for_loop.adb" compilation error

What's wrong with the code?


Solution

  • From this post:

    The problem is that 'A' and 'Z' could be from either Character or Wide_Character. The simplest correction is to make the type explicit; e.g.: for Char in Character range 'A' .. 'Z' loop ... end loop;

    with Ada.Text_IO; 
    use Ada.Text_IO;
    
    procedure for_Loop is
    begin
        for Counter in Character range 'A'..'Z' loop
            Put(Counter);   
        end loop;
        New_Line;
    end for_Loop;
    

    This is the oupput:

    ABCDEFGHIJKLMNOPQRSTUVWXYZ