【问题标题】:Ada compiler chooses incorrect overloaded functionAda 编译器选择了错误的重载函数
【发布时间】:2015-01-19 00:19:40
【问题描述】:

我正在使用avr-ada 来编译我的程序。我想将unsigned_16 变量右移两次。 Interfaces.Shift_Right 被重载,同时提供Unsigned_8Unisgned_16

当我编译时,我得到错误'expected type "Interfaces.Unsigned_8"','found type "Interfaces.Unsigned_16"'。我试图指定输入是Unsigned_16,但它不起作用。如何指定正确的函数?

with AVR;                          use AVR;
with AVR.MCU;
with AVR.Timer0;
with AVR.ADC;
with Interfaces;
use Interfaces;
procedure Flash_Led is

   adc_result_10 : Unsigned_16 := 0;
   adc_result_8 : Unsigned_8 := 0;

begin
   -- set OC0A pin as output, required for output toggling
   MCU.DDRD_Bits := (others => DD_Output);
   -- set all pins low
   MCU.PortD := 16#00#;

   -- clear register
   MCU.TCCR0B := 16#00#;

   --  initialize timer to Clear Timer on Compare, scale the input
   --  clock and set a value to compare the timer against.
   Timer0.Init_CTC (Timer0.Scale_By_1024, Overflow => 1);

   --  Toggle the OC0(A)-Pin on compare match
   Timer0.Set_Output_Compare_Mode_Toggle;

   -- Initialize ADC
   ADC.Init(ADC.Scale_By_64, Ref => ADC.Is_Vcc);


   loop   -- loop forever
      adc_result_10 := ADC.Convert_10bit(Ch => 0);
      adc_result_10 := Shift_Right(Unsigned_16'(adc_result_10), 2); --'
      adc_result_8 := Unsigned_8(adc_result_10);
      Timer0.Set_Overflow_At(adc_result_10);
   end loop;
end Flash_Led;

【问题讨论】:

  • 你打电话给Right_Shift还是Shift_Right?请更新您的问题以显示说明问题的(小)完整程序。
  • This 在 Linux 上使用 GNAT 编译没有错误(指的是 pastebin 上的示例,而不是问题中的代码)。

标签: ada gnat


【解决方案1】:

行:

Timer0.Set_Overflow_At(adc_result_10);

应该是:

Timer0.Set_Overflow_At(adc_result_8);

我没有仔细查看错误消息中的行号。哎呀。

【讨论】:

    【解决方案2】:

    如果您不确定正在调用哪个函数(假设它已重载),您始终可以通过重命名来构造一个唯一的名称...

    function My16_Shift_Right (Item : in Unsigned_16; Amount : Natural) return Unsigned_16 
        renames Interfaces.Shift_Right;
    

    然后测试您对程序的思考方式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多