-
Default Arguments
Everywhere that I've looked, it's told me that I'm doing this right. The compiler continues to disagree with me. Here's the code.
In random.h
PHP Code:
void new_random(unsigned long initial_seed = 0);
In random.cpp
PHP Code:
#include "random.h"
void new_random(unsigned long initial_seed)
{
//do stuff
}
I get three errors, all having to do with the function declaration in the header.
e:\servermodel\random.h(26) : error C2143: syntax error : missing ')' before '='
e:\servermodel\random.h(26) : error C2072: new_random' : initialization of a function
e:\servermodel\random.h(26) : error C2059: syntax error : ')'
I'm using VC++6 (and oh what a wonderful tool THAT is...). What am I doing wrong?
-
Try
Code:
void new_random(unsigned long initial_seed = 0)
{
//do stuff
}
-
pianorain,
Your code looks ok to me, I think, although I may be missing something.
-
It works fine for me in VC++ 6. Could you post some more of your code so we can get a better picture of what you're doine?
-Prelude
-
Sure...I can always post more code. This is only one part of a much larger project, but I've already gone through and checked to see if this function was called anywhere else or if the prototype exists in any other headers. It doesn't yet, but it probably will eventually, so I haven't made it a static function yet.
In random.h
PHP Code:
//// Includes ///////////////////////////////////////////////////////////
#include <ctime>
//// Prototypes /////////////////////////////////////////////////////////
void new_srandom(unsigned long initial_seed = 0);
static unsigned long new_random();
int rand_number(int from, int to);
int dice(int num, int size);
//// Constants ///////////////////////////////////////////////////////////
static const unsigned long m = 2147483647;
static const unsigned long q = 127773;
static const unsigned int a = 16807;
static const unsigned int r = 2836;
//// Globals ///////////////////////////////////////////////////////////
static unsigned long seed;
In random.cpp
PHP Code:
#include "random.h"
//// Definitions ///////////////////////////////////////////////////////
void new_srandom(unsigned long initial_seed)
{
seed = (initial_seed == 0) ? (unsigned)time(NULL) : initial_seed;
}
unsigned long new_random()
{
int lo, hi, test;
hi = seed/q;
lo = seed%q;
test = a*lo - r*hi;
if (test > 0)
seed = test;
else
seed = test+ m;
return (seed);
}
/* creates a random number in interval [from;to] */
int rand_number(int from, int to)
{
/* error checking in case people call this incorrectly */
if (from > to) {
int tmp = from;
from = to;
to = tmp;
//inform of error
}
return ((new_random() % (to - from + 1)) + from);
}
/* simulates dice roll */
int dice(int num, int size)
{
int sum = 0;
if (size <= 0 || num <= 0)
return (0);
while (num-- > 0)
sum += rand_number(1, size);
return (sum);
}
Much of this code is already public domain. I don't know if it works. If it does, I'm not sure why it works. All I'm trying to do is put a default argument on new_srandom().
BTW, I typed it in wrong the first time. The function is new_srandom(), not new_random().
-
Unfortunately, I've experienced much the same when trying to run code from lcc-win32 in an MSVC project. The compilers just don't accept the definitions in the same way.. so I made do without default arguments. Maybe somebody with a copy of the much referenced standard can help straighten things out?
-
Ah, I found the problem, even though I don't understand it. The function declaration was in random.h and the function definition was in random.c. When I changed it to random.cpp, everything worked. I didn't start out with much knowledge of C. Are default arguments not allowed in C?
-
Im not entirely sure, but when I was learning C, The book I used did not mention of default arguments.
-
pianorain, default arguments are a C++ thing. I'm sure your compiler just assumed that you wanted to compile random.c as a C source file (that is a normal default setting). All compilers have some sort of override for this sort of thing, nevertheless, I'd recommend not using the .c extension for a C++ source file.
-
>When I changed it to random.cpp, everything worked.
That explains it then, VC++ differentiates .c and .cpp extensions to mean a strictly C program and a C++ program, respectively. C doesn't support half as many neat little features as C++, including default arguments.
-Prelude