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)

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
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

Mar 24 2008