www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - "Error: function expected before (), not module *module* of type void

reply Dustin Mays <dustin.biggins.mays gmail.com> writes:
I'm getting the following error message when trying to compile a test-app I
wrote:
diceroller.d(9): Error: function expected before (), not module rollDice of
type void

Here is my code:
diceroller.d:
module diceroller;

import tango.io.Stdout;
import rollDice;


void main()
{
    Stdout (rollDice(6)).newline;
}

rollDice.d:
module rollDice;
import tango.math.Random;

uint rollDice(uint sides)
{
    uint result = 0;
    Random dice_Roller;
    dice_Roller = new Random();
    result = dice_Roller.next(1, (sides + 1));
    return result;
}


When I put the rollDice function in diceroller.d, all is well, compiles fine,
and works as expected. But when I try to put it in a seperate module,
rollDice.d, it doesn't work anymore. Note: I'm a newbie just learning D, and
I'm pretty new with programming in general, so I don't quite know what's going
on. :p 

Note: I think I should explain the following bit of code real quick: result =
dice_Roller.next(1, (sides + 1));. As I understand it, Random.next(uint min,
uint max) returns a value x where min <= x < max. So when Random.next(1,6) is
called, you'll only get values between 1-5, since x can't equal the max value.
I used "result = dice_Roller.next(1, (sides + 1))" so that I could write a more
natural interface, as calling rollDice(7) to simulate a six-sided die would
seem awkward.

Thanks in advance for any advice. :)

Dustin Mays
Mar 24 2008
next sibling parent Dustin Mays <dustin.biggins.mays gmail.com> writes:
Dustin Mays Wrote:
 *snip*
 uint rollDice(uint sides)
Huh, seems I just fixed my problem. I renamed this function and everything started working. Thanks anyway.
Mar 24 2008
prev sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
"Dustin Mays" wrote
 I'm getting the following error message when trying to compile a test-app 
 I wrote:
 diceroller.d(9): Error: function expected before (), not module rollDice 
 of type void
I know you fixed the problem, but just an FYI, the reason is because when you import rollDice, you bring both rollDice the module and rollDice the function into the global namespace (which confuses the compiler 'cause it doesn't know what symbol you want to use). This is normally avoided in libraries by having a package tree. So for example, if you created everything in the subdirectory foo, and had your modules be: module foo.diceroller; import foo.rollDice; Then the import would import the module foo.rollDice, and the function rollDice, and the compiler would no longer be confused about what you are trying to call. IMO, this makes it difficult to write multi-file applications that live in one directory. It would be nice if this was changed... -Steve
Mar 24 2008
next sibling parent Dustin Mays <dustin.biggins.mays gmail.com> writes:
Steven Schveighoffer Wrote:

 "Dustin Mays" wrote
 I'm getting the following error message when trying to compile a test-app 
 I wrote:
 diceroller.d(9): Error: function expected before (), not module rollDice 
 of type void
I know you fixed the problem, but just an FYI, the reason is because when you import rollDice, you bring both rollDice the module and rollDice the function into the global namespace (which confuses the compiler 'cause it doesn't know what symbol you want to use). This is normally avoided in libraries by having a package tree. So for example, if you created everything in the subdirectory foo, and had your modules be: module foo.diceroller; import foo.rollDice; Then the import would import the module foo.rollDice, and the function rollDice, and the compiler would no longer be confused about what you are trying to call. IMO, this makes it difficult to write multi-file applications that live in one directory. It would be nice if this was changed... -Steve
Thanks for the explanation, that confirmed what I thought was happening. :)
Mar 24 2008
prev sibling parent reply Samir <samir aol.com> writes:
On Monday, 24 March 2008 at 17:41:11 UTC, Steven Schveighoffer 
wrote:
 I know you fixed the problem, but just an FYI, the reason is 
 because when you import rollDice, you bring both rollDice the 
 module and rollDice the function into the global namespace 
 (which confuses the compiler 'cause it doesn't know what symbol 
 you want to use).  This is normally avoided in libraries by 
 having a package tree.  So for example, if you created 
 everything in the subdirectory foo, and had your modules be:

 module foo.diceroller;
 import foo.rollDice;

 Then the import would import the module foo.rollDice, and the 
 function rollDice, and the compiler would no longer be confused 
 about what you are trying to call.

 IMO, this makes it difficult to write multi-file applications 
 that live in one directory.  It would be nice if this was 
 changed...

 -Steve
I know this thread is quite old but I still seem to be getting a similar error and don't understand how to resolve it. I currently have a program isPrime.d that I would like to reuse in other programs: isPrime.d: bool isPrime(int n) { // logic to check if n is prime } main.d: import isPrime; void main() { isPrime(x); } Both files are in the same directory. When compiling main.d, I get: Error: function expected before (), not module isPrime of type void I've tried changing the name of the function isPrime in isPrime.d to something else (as well as changing the name in the main program) but then I get an error similar to: In function `_Dmain': main.d:(.text._Dmain[_Dmain]+0x83): undefined reference to `_D7isPrime3isPFiZb' collect2: error: ld returned 1 exit status Error: linker exited with status 1 Thanks in advance.
Sep 21 2018
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Saturday, 22 September 2018 at 01:51:33 UTC, Samir wrote:
 main.d:
 import isPrime;
 void main() {
     isPrime(x);
 }
You probably shouldn't name a module the same as a member anyway, and it should also have two names, like "module myproject.isprime;" But the fix here is to just use the full name. import isPrime; void main() { isPrime.isPrime(x); // module_name.member_name } or change the import: import isPrime : isPrime; // specify you want the same-named member
 Both files are in the same directory. When compiling main.d,
When compiling, be sure to pass both modules to it, or use the dmd -i if on a new version. dmd -i main.d or dmd main.d isPrime.d
 main.d:(.text._Dmain[_Dmain]+0x83): undefined reference to 
 `_D7isPrime3isPFiZb'
this likely means you forgot to compile in the isPrime module, so use the above dmd lines
Sep 21 2018
parent reply Samir <samir aol.com> writes:
On Saturday, 22 September 2018 at 01:58:57 UTC, Adam D. Ruppe 
wrote:
 You probably shouldn't name a module the same as a member 
 anyway, and it should also have two names, like "module 
 myproject.isprime;"

 But the fix here is to just use the full name.

 import isPrime;
 void main() {
   isPrime.isPrime(x); // module_name.member_name
 }

 or change the import:

 import isPrime : isPrime; // specify you want the same-named 
 member

 Both files are in the same directory. When compiling main.d,
When compiling, be sure to pass both modules to it, or use the dmd -i if on a new version. dmd -i main.d or dmd main.d isPrime.d
 main.d:(.text._Dmain[_Dmain]+0x83): undefined reference to 
 `_D7isPrime3isPFiZb'
this likely means you forgot to compile in the isPrime module, so use the above dmd lines
Thanks for your help, Adam! Right after posting my question, I started reading this site: https://www.tutorialspoint.com/d_programming/d_programming_modules.htm Based on that and your recommendation, here is what I ended up doing: I changed the filename of isPrime.d to isprime.d and put that in the subdirectory func/: func/isprime.d: module func.isprime; bool isPrime(int n) { // check to see if n is prime } I then changed main.d to: import func.isprime; void main() { isPrime(x); } Finally, per your suggestion, I compiled it using: dmd -i main.d Thanks again!
Sep 21 2018
parent ag0aep6g <anonymous example.com> writes:
On 09/22/2018 04:51 AM, Samir wrote:
 Thanks for your help, Adam!  Right after posting my question, I started 
 reading this site:
 https://www.tutorialspoint.com/d_programming/d_programming_modules.htm
Better read the original: http://ddili.org/ders/d.en/modules.html
Sep 22 2018