CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Jun 2003
    Posts
    86

    default arguments

    Hi,
    I want to create a function with default arguments.

    e.g.
    void fun(int i=90,j=100)
    {
    }

    void main()
    {
    fun();
    fun(34);
    fun(50,60);
    }

    but it is not working and the complaier tells me that I don't give enough arguments.

    any idea?
    rgrds megetron.

  2. #2
    Join Date
    Jul 2003
    Location
    Singapore
    Posts
    1,822
    huh?????
    i dont understand wat u want...can u explain a bit more..exactly wat do you want???
    R. Thomas
    "Be anxious for nothing, but in everything by prayer and supplication, with thanksgiving, let your requests be made know to God; and the peace of God, which surpasses all understanding, will guard your hearts and minds through Christ Jesus."Philippians 4:6-7
    "Rejoice always, pray without ceasing, in everything give thanks; for this is the will of God in Christ Jesus for you."1Thess. 5:16-18

  3. #3
    Join Date
    Jun 2003
    Posts
    86
    I want to call a function but this function should not force me insert arguments. if I will decide to insert parameters, fine .
    if i will decide not insert parameters than the function will use my default parameters and values.

    for example look at the function of microsoft (CDialog):

    virtual BOOL Create(
    LPCTSTR lpszTemplateName,
    CWnd* pParentWnd = NULL
    );

    I can call this function in 2 ways:
    Cdialog dlg;
    dlg.Create("blabla");
    //or
    CWnd* m_Wnd;
    dlg.Create("blabla",m_Wnd);

  4. #4
    Join Date
    May 2001
    Location
    Madrid-Spain
    Posts
    1,123
    Example from MSDN:
    Code:
    /* VA.C: The program below illustrates passing a variable
     * number of arguments using the following macros:
     *      va_start            va_arg              va_end
     *      va_list             va_dcl (UNIX only)
     */
    
    #include <stdio.h>
    #include <stdarg.h>
    int average( int first, ... );
    
    void main( void )
    {
       /* Call with 3 integers (-1 is used as terminator). */
       printf( "Average is: %d\n", average( 2, 3, 4, -1 ) );
    
       /* Call with 4 integers. */
       printf( "Average is: %d\n", average( 5, 7, 9, 11, -1 ) );
    
       /* Call with just -1 terminator. */
       printf( "Average is: %d\n", average( -1 ) );
    }
    
    /* Returns the average of a variable list of integers. */
    int average( int first, ... )
    {
       int count = 0, sum = 0, i = first;
       va_list marker;
    
       va_start( marker, first );     /* Initialize variable arguments. */
       while( i != -1 )
       {
          sum += i;
          count++;
          i = va_arg( marker, int);
       }
       va_end( marker );              /* Reset variable arguments.      */
       return( sum ? (sum / count) : 0 );
    }
    I am Miss Maiden... Miss Iron Maiden :-D

  5. #5
    Join Date
    Jul 2003
    Location
    Singapore
    Posts
    1,822
    Default values in arguments.
    When declaring a function we can specify a default value for each parameter. This value will be used if that parameter is left blank when calling to the function. To do that we simply have to assign a value to the arguments in the function declaration. If a value for that parameter is not passed when the function is called, the default value is used, but if a value is specified this default value is stepped on and the passed value is used. For example:
    Code:
    // default values in functions
    #include <iostream.h>
    int divide (int a, int b=2)
    {
      int r;
      r=a/b;
      return (r);
    }
    int main ()
    {
      cout << divide (12);
      cout << endl;
      cout << divide (20,4);
      return 0;
    }
    As we can see in the body of the program there are two calls to the function divide. In the first one:
    divide (12)
    we have only specified one argument, but the function divide allows up to two. So the function divide has assumed that the second parameter is 2 since that is what we have specified to happen if this parameter is lacking (notice the function declaration, which finishes with int b=2). Therefore the result of this function call is 6 (12/2).
    In the second call:
    divide (20,4)
    there are two parameters, so the default assignation (int b=2) is stepped on by the passed parameter, that is 4, making the result equal to 5 (20/4).

    you can find the above
    here
    hope that helps..
    God Bless
    R. Thomas
    "Be anxious for nothing, but in everything by prayer and supplication, with thanksgiving, let your requests be made know to God; and the peace of God, which surpasses all understanding, will guard your hearts and minds through Christ Jesus."Philippians 4:6-7
    "Rejoice always, pray without ceasing, in everything give thanks; for this is the will of God in Christ Jesus for you."1Thess. 5:16-18

  6. #6
    Join Date
    Jun 2003
    Posts
    86
    Joseph_R_Thomas. this is exactly what I done and compiler tells me that the function do not except the number of parameters that i supplied.
    maybe it is because I am using const CString, and not int?
    here is the exact code:

    BOOL CQAolDlg::FileLink(const CString strPathObj,const CString strPathLink, const CString strArg=NULL)
    {
    return TRUE;
    }

    and here i call the func:
    FileLink("bla","blabla");
    FileLink("bla","blabla","blablabla);

    irona20. this method seems to be intresting i didn't know her before, but i thinkk this is good when you have alot of parameters and the number of parameters are unknown. in my case i know exaxtly how many parameters I have, so i dont need to enter to a loop.
    if no other option will be found I think I will use it.

  7. #7
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Originally posted by megetron
    BOOL CQAolDlg::FileLink(const CString strPathObj,const CString strPathLink, const CString strArg=NULL)
    {
    return TRUE;
    }
    Besides that you should pass 'CString' rather by reference than by value...what is the 'NULL' supposed to do for the last argument?

    Do you want an empty string by default? In this case....
    Code:
    BOOL CQAolDlg::FileLink(const CString strPathObj,const CString strPathLink, const CString strArg=CString())
    {
    return TRUE;
    }

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449
    Code:
    BOOL FileLink(const CString strPathObj,const CString strPathLink, const CString strArg=NULL)
    {
        return TRUE;
    }
    This line doesn't even compile, and the error isn't what you described. It is illegal to convert a CString to an "int", and that is what NULL is -- an integer. That is the error that the compiler flags, not that the number of parameters don't match.
    error C2440: 'default argument' : cannot convert from 'const int' to 'const class CString'
    No constructor could take the source type, or constructor overload resolution was ambiguous
    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; August 25th, 2003 at 02:02 PM.

  9. #9
    Join Date
    Jun 2003
    Posts
    86
    Paul McKenzie. you are right. the line does not compile, so i have changed it into:
    BOOL FileLink(const CString strPathObj,const CString strPathLink,
    const CString strArg="Text")
    {
    return TRUE;
    }
    but still the line: FileLink("bla","blabla"); does not compiled because the number of parameters are unadequate:
    error C2660: 'FileLink' : function does not take 2 parameters

    Andreas Masur. I dont need an empty string. I need to post 2 parameters to a function that takes 3 parameters.
    I changed the code into:
    BOOL FileLink(const CString strPathObj,const CString strPathLink,
    const CString strArg="Text")

    the error is the same as I described above.

  10. #10
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Well...can you attach your project or a sample project which is compilable and reproduces the behaviour?

  11. #11
    Join Date
    Jun 2003
    Posts
    86

    Sure.

    no problem: here is the attachment of the project.
    Attached Files Attached Files

  12. #12
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Well...the problem is simple....you did not specify a default parameter in your function 'FileLink()'...

    Add it within the declaration in the 'CQAolDlg' class ('CQAolDlg.h')...
    Code:
    BOOL FileLink(const CString strPathObj,const CString strPathLink, const CString strArg="Text")

  13. #13
    Join Date
    Jun 2003
    Posts
    86

    Cool

    ok. now it is working. so actually when i want to do the above i just need to do this in the header file.
    10x. it was helpfull.
    rgrds all, megetron.

  14. #14
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Originally posted by megetron
    ok. now it is working. so actually when i want to do the above i just need to do this in the header file.
    Yes...default parameter are only provided with the declaration of a function but not with the definition (which would result in a compiler error)...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured