Default Arguments

This is a discussion on Default Arguments within the C++ Programming forums, part of the General Programming Boards category; Everywhere that I've looked, it's told me that I'm doing this right. The compiler continues to disagree with me. Here's ...

  1. #1
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399

    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?
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  2. #2
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Try
    Code:
    void new_random(unsigned long initial_seed = 0)
    {
      //do stuff
    }

  3. #3
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    pianorain,

    Your code looks ok to me, I think, although I may be missing something.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    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
    My best code is written with the delete key.

  5. #5
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    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 fromint to);
    int dice(int numint 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 lohitest;

        
    hi   seed/q;
        
    lo   seed%q;

        
    test a*lo r*hi;

        if (
    test 0)
        
    seed test;
        else
        
    seed testm;

        return (
    seed);
    }

    /* creates a random number in interval [from;to] */
    int rand_number(int fromint 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 numint size)
    {
      
    int sum 0;

      if (
    size <= || num <= 0)
        return (
    0);

      while (
    num-- > 0)
        
    sum += rand_number(1size);

      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().
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  6. #6
    ggs
    ggs is offline
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    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?
    .sect signature

  7. #7
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    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?
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  8. #8
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    Im not entirely sure, but when I was learning C, The book I used did not mention of default arguments.
    C Code. C Code Run. Run Code Run... Please!

    "Love is like a blackhole, you fall into it... then you get ripped apart"

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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.

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Utilizing another compiled program for a task.
    By kotoroshinoto in forum C Programming
    Replies: 6
    Last Post: 06-03-2008, 02:43 PM
  2. Default arguments
    By swgh in forum C Programming
    Replies: 5
    Last Post: 06-29-2007, 07:27 PM
  3. default Arguments
    By kishore in forum C++ Programming
    Replies: 2
    Last Post: 01-06-2007, 02:17 AM
  4. Default Arguments Question...
    By tinkerbell20 in forum C++ Programming
    Replies: 4
    Last Post: 07-07-2005, 06:27 PM
  5. pointers as default arguments
    By ajm in forum C++ Programming
    Replies: 3
    Last Post: 07-09-2002, 04:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21