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.
Printable View
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.
huh?????
i dont understand wat u want...can u explain a bit more..exactly wat do you want???
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);
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 );
}
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:
As we can see in the body of the program there are two calls to the function divide. In the first one: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;
}
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
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.
Besides that you should pass 'CString' rather by reference than by value...what is the 'NULL' supposed to do for the last argument?Quote:
Originally posted by megetron
BOOL CQAolDlg::FileLink(const CString strPathObj,const CString strPathLink, const CString strArg=NULL)
{
return TRUE;
}
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;
}
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.Code:BOOL FileLink(const CString strPathObj,const CString strPathLink, const CString strArg=NULL)
{
return TRUE;
}
Regards,Quote:
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
Paul McKenzie
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.
Well...can you attach your project or a sample project which is compilable and reproduces the behaviour?
no problem: here is the attachment of the project.
Well...the problem is simple....you did not specify a default parameter in your function 'FileLink()'... :cool:
Add it within the declaration in the 'CQAolDlg' class ('CQAolDlg.h')...
Code:BOOL FileLink(const CString strPathObj,const CString strPathLink, const CString strArg="Text")
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.
Yes...default parameter are only provided with the declaration of a function but not with the definition (which would result in a compiler error)...Quote:
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.