I've having trouble with package visibility. I have a really simple package and the code is listed below. The error message is shown here:
viterbi.adb:12:14: "Integer_Text_IO" is not visible (more references follow)
viterbi.adb:12:14: non-visible declaration at a-inteio.ads:18
gnatmake: "viterbi.adb" compilation error
The package specification is as follows:
package Viterbi is
procedure Load_N_File(
Filename : in String;
N : in out Integer;
M : in out Integer);
end Viterbi;
The package body is as follows:
with Ada.Integer_Text_IO; use with Ada.Integer_Text_IO;
with Ada.Strings; use Ada.Strings;
package body Viterbi is
procedure Load_N_File(
Filename : in String;
N : in out Integer;
M : in out Integer
) is
N_File : File_Type;
begin
Open( N_File, Mode=>In_File, Name=>Filename );
Get( N_File, N );
Get( N_File, M );
Close( N_File );
end Load_N_File;
end Viterbi;
What in my package body is causing the package to stay hidden? Shouldn't the use clause bring Integer_Text_IO into view?
The code of the package body as provided has a syntax error: the spurious "with" in the "use with Ada.Integer_Text_IO;" clause.
Having fixed that, I then get compilation errors revolving around the inability to resolve File_Type, Open, and Close. Adding a "with" and "use" of Ada.Text_IO gives me a clean compilation.
So the start of the package body looks like:
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Strings; use Ada.Strings;
with Ada.Text_IO; use Ada.Text_IO;
package body Viterbi is
...
If you're still getting a "can't find Integer_Text_IO" error after fixing these errors, then I'd be suspicious about your development environment, i.e. is everything installed correctly?
You can avoid the "use with" style of error, as already pointed out, by using the comma-separated style:
With
-- Testing,
Ada.Integer_Text_IO,
Ada.Strings;
Use
-- Testing,
Ada.Strings,
Ada.Integer_Text_IO;
this also allows you to comment out specific package 'withs' or 'usues' as shown.
Related
Page 53 of "Programming in Ada 2012" by John Barnes shares an incomplete fragment of code that I cannot get to work.
I've come up with this as a full program expanding the code from the book...
with Ada.Numerics; use Ada.Numerics;
with Ada.Text_IO; use Ada.Text_IO;
procedure Main is
type Coin is (Heads, Tails);
package Random_Coin is new Discrete_Random(Coin);
use Random_Coin;
G : Generator;
C : Coin;
begin
for i in 1 .. 20 loop
C := Random(G);
Put (C'Image);
end loop;
end Main;
The "GPS" IDE I'm using complains of the following errors:
Line 6: "Discrete_Random" is undefined
Line 7: "Random_Coin" is undefined
Line 9: "Generator" is undefined
Line 14: "Random" is undefined
The IDE does give me the "intellisense" (to use a term from Visual Studio) that indicates that Discrete_Random is actually visible and available given the "with" and "use" statements I've added.
Can someone walk me through the stupid mistakes I'm making please?
The problem is that, unlike Ada.Numerics.Pi, in which Pi is a component of Ada.Numerics, Discrete_Random is a child of Ada.Numerics.
Once you’ve said use Ada.Numerics, you could write just Pi in your program, but you have to actually with Ada.Numerics.Discrete_Random in order for it to be available.
In fact, you don’t need to with or use Ada.Numerics, this works just fine:
with Ada.Numerics.Discrete_Random;
with Ada.Text_IO; use Ada.Text_IO;
procedure Main is
type Coin is (Heads, Tails);
package Random_Coin is new Ada.Numerics.Discrete_Random(Coin);
I'm trying to do some basic translations of old C++ code from many moons ago to learn Ada, and I have been absolutely stumped on how to sort a vector using the built-in Generic_Sorting. I haven't been able to find any concrete examples of it in action, the closest being a now-defunct Danish wiki article which looks like it would have had a full example but the archive didn't snatch it up: https://web.archive.org/web/20100222010428/http://wiki.ada-dk.org/index.php/Ada.Containers.Vectors#Vectors.Generic_Sorting
Here is the code as I thought it should work from the above link:
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Containers.Vectors; use Ada.Containers;
procedure Vectortest is
package IntegerVector is new Vectors
(Index_Type => Natural,
Element_Type => Integer);
package IVSorter is new Generic_Sorting;
IntVec : IntegerVector.Vector;
Cursor : IntegerVector.Cursor;
begin
IntVec.Append(3);
IntVec.Append(43);
IntVec.Append(34);
IntVec.Append(8);
IVSorter.Sort(Container => IntVec);
Cursor := IntegerVector.First(Input);
while IntegerVector.Has_Element(Cursor) loop
Put(IntegerVector.Element(Cursor));
IntegerVector.Next(Cursor);
end loop;
end Vectortest;
I've tried so many different combinations of use and with but all I can get are various error codes. The above code gives Generic_Sorting is not visible, but when I try to explicitly state with Ada.Containers.Vectors.Generic_Sorting I get the error "Ada.Containers.Vectors.Generic_Sorting" is not a predefined library unit. I have no idea what I'm doing wrong here, I'm sure it's a fundamental misunderstanding of the way Ada brings in packages, and I hope that nailing this down will help me understand it a bit better.
Generic_Sorting is an inner package to Ada.Containers.Vectors and cannot be explicitly withed (as you have discovered). And since Ada.Containers.Vectors is itself a generic package, Generic_Sorting is an inner package to the instantiation of Ada.Containers.Vectors. So you can reach it by prefixing with the name of the instance:
package IVSorter is new IntegerVector.Generic_Sorting;
I have a package called Linked_List(.ads) Here is the code in it:
Generic
type T is private;
package Linked_List is
type List is tagged record
Data : T;
end record;
end Linked_List;
and here is the code in the package which contains the main function (main.adb)
with Ada.Text_IO; use Ada.Text_IO;
with Linked_List;
procedure Main is
type ListOfIntegers is new Linked_List(T => Integer);
begin
null;
end Main;
I'm getting this error:
4:30 subtype mark required in this context
found "Linked_List" declared at linked_list.ads:3
found "Linked_List" declared at linked_list.ads:3
4:41 incorrect constrain for this kind of type
Any help is appreciated.
new Linked_List(T => Integer) defines a new package, not a new type. The error messages you’re getting are because the compiler thinks you’re declaring a type, so seeing the name of a package at column 30 confused it; it wanted to see the name of a (sub)type.
Line 4 should read
package ListOfIntegers is new Linked_List(T => Integer);
after which there is a type ListOfIntegers.List, so you can write
My_List : ListOfIntegers.List;
You may find having to say ListOfIntegers. all the time annoying; you can say
use ListOfIntegers;
after which you can just write
My_List : List;
but it’s usually thought best not to overdo this (if you have dozens of “withed” packages, “using” them all makes it difficult to know which one you’re referring to).
By the way, normal Ada usage is to use underscores to separate words in identifiers: List_Of_Names.
I want to use a package spec inside a procedure.
Something is missing for that, but i don't know what.
using_ads_package.adb:14:11: "var" is not visible
using_ads_package.adb:14:11: non-visible declaration at line 8
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Text_IO.Unbounded_IO; use Ada.Text_IO.Unbounded_IO;
Procedure using_ads_package is
Package variable is
var : Unbounded_String ;
end variable ;
Begin
get_line(var);
End using_ads_package ;
In
procedure Using_Ads_Package is
package Variable is
Var : Unbounded_String;
end Variable;
begin
you’ve created a new scope in package Variable. Outside Variable, Var isn’t directly visible.
Outside Variable, you have two ways of referring to Var.
First, you can name the package in which it’s declared explicitly:
Get_Line (Variable.Var);
Alternatively, you can use Variable:
package Variable is
Var : Unbounded_String;
end Variable;
use Variable;
begin
Get_Line (Var);
Without compiling I’d suggest that the line:
get_line(var);
should be:
get_line(variable.var);
I am just a beginner in Ada,
my code looks like this,
with Ada.Strings.Unbounded;
use Ada.Strings.Unbounded;
procedure final is
Input : Unbounded.String;
begin
null;
end final;
When i compile this code with gnatmake,
compiler gives error "Unbounded" is not visible", what does this mean?
The use clause for Ada.Strings.Unbounded makes declarations inside that package directly visible. It does not make the package name itself (Unbounded) directly visible, so any reference to the name Unbounded that's not preceded by a . is going to be incorrect.
Furthermore, the type is called Unbounded_String, not String.
Change this:
Input : Unbounded.String;
to this:
Input : Unbounded_String;
(And please indent your code.)