I have a problem, and "googling" ddidn't find nothing..
myu problem is that i have a function such
codeguru(int i, int y, int z)
{
cout << i, y, z;
}
now the function is right!!..
but i don't wont to pass the y variable,
so i can do :
codeguru(int i, int y=3 , int z)
{
cout << i, y, z;
}
but when i call it, whith
codeguru(2, ,4);
isn't write..
why is wrong??..
how can i make it better?
laitinen
April 3rd, 2008, 07:38 AM
The default parameters need to be the right-most parameters in the function.
void codeguru(int i, int z, int y=3)
{...}
Laitinen
GNiewerth
April 3rd, 2008, 07:41 AM
You can only provide default parameters from right to left, you cannot omit the center ones. When calling a function with default parameters you simply omit the parameters you donīt want to specify, you cannot use placeholders. In your example you have to specify both y and z, because z is the rightmost parameter and it has no default value, so you must specify z. Once you specify z you must specify y, too, because y is left of z.
You can reorder your parameters and move the ones which will most likely use default values to the right.
Rooting
April 4th, 2008, 06:13 AM
i need to assign a temporaney variable, becouse when i use this function
i'm free to ommit the variable...
i use how you told me..
#include <cstdlib>
#include <iostream>
using namespace std;
void function(int x, int y=3)
{
cout << x << endl << y;
}
int main(int argc, char *argv[])
{
function(2,);
system("PAUSE");
return EXIT_SUCCESS;
}
but there are errors in this line:
function(2,);
this is an error becouse i don't pass the value of variable,
but in function i asssign it!!...
if i don't wont to assign when i call, how i can do??..
laserlight
April 4th, 2008, 06:17 AM
The function call should be: function(2);
Graham
April 4th, 2008, 06:49 AM
cout << i, y, z;
It's entirely possible that that line does not do what you think it does.
Rooting
April 4th, 2008, 07:03 AM
It's entirely possible that that line does not do what you think it does.
i try not this source but this one:
#include <cstdlib>
#include <iostream>
using namespace std;
void function(int x, int y=3)
{
cout << x << endl << y;
}
int main(int argc, char *argv[])
{
function(2,);
system("PAUSE");
return EXIT_SUCCESS;
}
in this source is right!!..
but is wrong when i call the function..
Lindley
April 4th, 2008, 07:19 AM
The function call should be: function(2);
Repeated.
Rooting
April 4th, 2008, 09:55 AM
mmm...ok..
when i have 2 variable now is ok..
but, in my case i have 3 variable as:
#include <cstdlib>
#include <iostream>
using namespace std;
void function(int x=1, int y=3, int z = 4)
{
cout << x << endl << y << endl << z;
}
int main(int argc, char *argv[])
{
function(,5,);
system("PAUSE");
return EXIT_SUCCESS;
}
in this case i have 3 variable, and i wold like to specify the y one..
the y is in the midle..
the expression
function(,5,) is wrong!.
how can i resolve??..
Lindley
April 4th, 2008, 10:07 AM
C++ doesn't support that. It can only supply default values to those parameters on the right of the ones you specify.
And you need to stop trying to use commas that don't separate anything. As far as I know that's always a syntax error.
laserlight
April 4th, 2008, 11:50 AM
And you need to stop trying to use commas that don't separate anything. As far as I know that's always a syntax error.
There is at least one exception where it is allowed, namely, when it comes after an initialiser list, e.g., {1, 2, 3,}. That does not apply here, though.
Graham
April 4th, 2008, 05:42 PM
Why not just overload the functions?
void func(int x, int y, int z)
{
cout << x << y << z;
}
void func(int y)
{
func(1, y, 2);
}
int main()
{
func(3);
}
Rooting
April 5th, 2008, 01:33 AM
mmm...
i'm making a library, so I don't know if the user can use the x or y or z variable...
understand me??..
so if the user don't specify the x variable, but put the y and z , the library must go!!..
it's the function of class..
overloading as graham sad is very simple, but not elegant, becouse i have 15 function in
this class..and i don't know if he specify the x or y or z one..
any suggestion??
code_carnage
April 5th, 2008, 02:19 AM
Perahps variadic function might help.. Do google to find out the implementation of variadic function...
laserlight
April 5th, 2008, 03:03 AM
I think that neither function overloading nor variadic functions will cut it here. What is needed is named arguments, which is not a feature of C++. The reason is that there is nothing to differentiate foo(x, y) from a call to foo(x, z) or foo(y, z).
I am not sure if this really is a good idea, but I would propose a workaround with std::map and some function templates:
#include <iostream>
#include <map>
mmm...
so i decide to use different function..
one whith x and y and one y and z whith the same name..
the class permit it , so itn't wrong!..
anyone have another idea?..
@laserlight : good idea!!..but is a litle bit hard..
thanks to all!!..
exterminator
April 5th, 2008, 07:02 AM
I think that neither function overloading nor variadic functions will cut it here. What is needed is named arguments, which is not a feature of C++. The reason is that there is nothing to differentiate foo(x, y) from a call to foo(x, z) or foo(y, z).
I am not sure if this really is a good idea, but I would propose a workaround with std::map and some function templates:
#include <iostream>
#include <map>
void foo(const std::map<char, int>& values)
{
std::map<char, int>::const_iterator iter = values.find('x');
int x = (iter == values.end()) ? 0 : iter->second;
iter = values.find('y');
int y = (iter == values.end()) ? 0 : iter->second;
iter = values.find('z');
int z = (iter == values.end()) ? 0 : iter->second;
std::cout << x << ' ' << y << ' ' << z << std::endl;
}
int main()
{
foo(make_values<'x'>(1));
foo(make_values<'z', 'y'>(10, 20));
foo(make_values<'y', 'x', 'z'>(2, 3, 4));
}
Hmm... std::vector<std::pair<char, T> > would have been easier too look at. But I think it's not worth the effort. This makes the calling code and the implementation (and not just the interface/prototype) too strongly coupled. Not worth it, no. The OP must accept the fact that what he wants does not make sense in C++ (and many other lnaguages as well). He just have to have default arguments in the order as it is supposed to be or pass the default argument values while making the call or have overloads if they can be implemented (needs default arguments to be of different types else overloading doesnt work either).
Otherwise, have some kind of a message format (XML or any other) if this too complicated layer of interaction and add logic to build and decode what the message holds (analogous to the map solution but more generic across application layers). But I don't think this is what he wants.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.