1. Need Help Creating include files.
- Posted by Martin Berger <digitronics2004 at yahoo.com> Feb 25, 2005
- 605 views
Hi all, I need to know what I am doing wrong here: I created an include file called var.e with the following type definitions. -- var.e include file -- Custom type definitions type POSITIVE(atom x) return x >= 0 end type type NEGATIVE(atom x) return x < 0 end type Then I included the file var.e in my program like so: include get.e include var.e -- the include file that I created. atom sum POSITIVE pos_num NEGATIVE neg_num sequence press pos_num = prompt_number("\nEnter a positve number only!!!: ", {}) neg_num = prompt_number("\nEnter a negaitve number only!!!: ", {}) sum = pos_num + neg_num puts(1,"\n" & "Thank You. The positive number you entered was ") ? pos_num puts(1,"\n" & "Thank You. The negative number you entered was ") ? neg_num puts(1,"\n" & "The result of adding the numbers is ") ? sum press = gets(0) When I try to execute the prog, I get this error: numinp2.ex:29 POSITIVE has not been declared POSITIVE pos_num ^ What am I missing? I'm brand new to programming in Euphoria. I'm using ver 2.4 on a Win XP machine, running in a DOS window. I put the file var.e in the euphoria\include directory. I set the environment variable and path according to instructions found on the web site. Any help would be appreciated. Thanks Martin
2. Re: Need Help Creating include files.
- Posted by Matt Lewis <matthewwalkerlewis at gmail.com> Feb 25, 2005
- 588 views
Martin Berger wrote: > > Hi all, > > I need to know what I am doing wrong here: > > I created an include file called var.e with the following type definitions. > > -- var.e include file > -- Custom type definitions > > > type POSITIVE(atom x) > return x >= 0 > end type > > type NEGATIVE(atom x) > return x < 0 > end type These need to be declared as global if you want to use them outside of the file in which they are declared.
global type POSITIVE(atom x) return x >= 0 end type global type NEGATIVE(atom x) return x < 0 end type
A type has scope just like a function, and can be called just like a regular function. Matt Lewis
3. Re: Need Help Creating include files.
- Posted by Martin Berger <digitronics2004 at yahoo.com> Feb 25, 2005
- 593 views
- Last edited Feb 26, 2005
Matt Let me see if I understand, Any function or type I put in an include file has to be declared as a global? Martin
4. Re: Need Help Creating include files.
- Posted by Brian Broker <bkb at cnw.com> Feb 25, 2005
- 609 views
- Last edited Feb 26, 2005
Martin Berger wrote: > > Hi all, > > I need to know what I am doing wrong here: > <snip> > What am I missing? I'm brand new to programming in Euphoria. > I'm using ver 2.4 on a Win XP machine, running in a DOS window. > > I put the file var.e in the euphoria\include directory. > I set the environment variable and path according to instructions found on > the web site. > > Any help would be appreciated. > > Thanks > > Martin > See section 2.4.2 (Scope) of the reference manual (Language Definition): When you 'include' a Euphoria file in a main file (see 2.6 Special Top-Level Statements), only the variables and routines declared using the 'global' keyword are accessible or even visible to the main file. The other, non- global, declarations in the included file are forgotten at the end of the included file, and you will get an error message, "not declared", if you try to use them in the main file. So to fix your problem, do this:
-- var.e include file -- Custom type definitions global type POSITIVE(atom x) return x >= 0 end type global type NEGATIVE(atom x) return x < 0 end type
-- Brian
5. Re: Need Help Creating include files.
- Posted by Matt Lewis <matthewwalkerlewis at gmail.com> Feb 25, 2005
- 582 views
- Last edited Feb 26, 2005
Martin Berger wrote: > > Matt > > Let me see if I understand, Any function or type I put in an include file > has to be declared as a global? Only if you want to allow code from other files to be able to use it. I know, it sounds like you'll want to make everything global, but as you write more complex include files, there will inevitably be plenty of stuff that you don't want to be global. In general, it's a good idea to keep as much local as possible, especially variables. Matt Lewis
6. Re: Need Help Creating include files.
- Posted by Martin Berger <digitronics2004 at yahoo.com> Feb 25, 2005
- 592 views
- Last edited Feb 26, 2005
To Matt & Brian: Thanks for responding, problem solved!!!!