void print(string str,int a=0)
{
cout<<str;
}
int main()
{
string str="hello world";
print(str);
return 0;
}
why is the code working if I'am only passing one argument whereas the function needs two arguments
Whenever you have a function with a defaulted argument
void some_func(int a, int def = 0)
{
//something
}
The following call
some_func(42);
is converted into
some_func(42, 0);
And you can also call the function with two arguments, such as some_func(42, 1);
It works because you defined this int on a function declaration. The compiler knows that a =0 and is an int type. However if you lets say call this function like this:
print(str,10);
int a will have value of 10 instead of 0.
You are declaring function with one argument to have a default initial value, that is why you are able to call function without that argument.
if not passing that argument to function, its initial value that you declared, is used.
Related
So, I have this code for example:
void doSomething(int aValue, int aValue2) {
/*thigs to do...*/
}
int main() {
doSomething(9);
}
Notice that I only used 9 for aValue and had aValue2 empty.
What I want is to automatically fill-in the missing aValue2 with a certain default value.
Is there any way to do that?
There are at least two ways to do it. The first way is to declare your function with a default-argument, like this:
void doSomething(int aValue, int aValue2 = 123);
... then when it is called with only one argument, the compiler will treat the call as if the second argument was supplied as 123. Note that the default value goes in the line where the function is declared, not where it is defined, so in cases where you have a separate .h file containing the function's declaration, the = 123 would go there and not in the .cpp file.
The other way would be to overload the function, like this:
void doSomething(int aValue, int aValue2) {
/*things to do...*/
}
void doSomething(int aValue) {
doSomething(aValue, 123);
}
This is a little wordier, but it gives you more flexibility in deciding how the first (two-argument) version of the function should be called when the caller supplies only one argument.
What you want is known as a default argument where you assign the value to the parameter in the function declaration or definition if you do not have a forward declaration for the function. In the below example the default argument for aValue2 is set to 100.
void doSomething(int aValue, int aValue2 = 100) {
/*thigs to do...*/
}
int main() {
doSomething(9);
}
Let's say you have a function like this:
int Function(int a = 5, int b = 10){
...
}
When you call the same function with fewer parameters then it should have, for example:
int var = Function(8);
'a' becomes 8, and 'b' gets the default value 10;
With this in mind, let's say I have another function called Function3 that receives another function as its argument:
int Function3(int x, int Function2 (int)){
...
int m = Function2(x);
...
return m;
}
How do I set the default function (function2) that should be called if I do not send any function as an argument to Function3 (using the lambda functions)?
So if I do this:
int ver = Function3(y);
instead of this:
int ver = Function3(y,std::sin)
How do I tell it to, for example, multiply the number with 3 if nothing has been sent as a third argument? (and if it has to use that function to work with x and y).
Your parameter should be a pointer to a function, and you can assign this like any other parameter.
int Function3(int a, int b, int (*func)(int,int) = Function2)
{
func(a,b); //call will use passed function, or Function2 if one wasn't provided
}
The way I do this is that I make the default value be NULL, and then in the implementation of the function I begin with an if statement which checks whether NULL was passed for that parameter, and if so, uses the intended default instead.
I am trying to pass parameters to a function pointer being passed as a parameter.
Code:
void Test(wchar_t* a, wchar_t* b)
{
// ...
}
void Test2(void(*Func)(wchar_t*, wchar_t*))
{
// ...
}
int main()
{
Test2(Test(L"Hello", L"Testing"));
return 0;
}
I am getting this error:
argument of type "void" is incompatible with parameter of type "void (*)(wchar_t *, wchar_t *)"
How do I fix this to accomplish what I'm trying to achieve?
Edit: Sorry for not being clear. What I'm actually trying to accomplish is inject a function into a child process and pass two parameters (wchar_t*, wchar_t*) so I can use them. But the main function can either be void or int argc, char** argv. So I accomplished what I'm trying to achieve by simply using global variables
You probably want to have something like
void Test2(void(*Func)(wchar_t*, wchar_t*),wchar_t* x, wchar_t* y)
{
(*Func)(x,y);
}
int main()
{
Test2(Test,L"Hello", L"Testing");
return 0;
}
instead.
As for your comment
How do i do this in C++ with templates?
I could think of
template<typename Param>
void Test2(void(*Func)(Param, Param), Param x, Param y) {
(*Func)(x,y);
}
void Test(wchar_t* a, wchar_t* b);
int main() {
Test2(Test,L"Hello", L"Testing");
return 0;
}
This should just work fine.
There are more than one way to fix tihs issue, however, let me just try to show why this error is occuring.
Every function has a type of value associated with it. This means, that every function evaluates to a value of some type. This is indicated by its return value.
For example:
int foo(/*whatever*/);
evaluates to an int. So foo(/*whatever*/) can be used anywhere an int is expected. For example like int a = b + foo(/*whatever*/).
Simlarly float bar(/*whatever*/); evaluates to a float, hence bar(/*whatever*/) can be used anywhere a float is expected. For example like float a = b + bar(/*whatever*/).
A function that returns void like void foobar(/*whatever*/) however, evaluates to void and cannot be used where a value of some type (say int, float, etc) is expected.
Now coming to code. This line in your main function has the issue:
int main()
{
Test2(Test(L"Hello", L"Testing")); /* Issue here */
return 0;
}
Here you are passing Test(L"Hello", L"Testing") as the argument to Test2. Now remember, that Test(/*whatever*/), evaluates to a void because Test returns a void.
So what you are doing in that line is something like
Test2(/*something that evaluates to a void*/);
However, Test2 expectes a void (*)(wchar_t*, wchar_t*), which is a pointer to a function that returns void, which is different from void.
So what is happening, is that the compiler is seeing that you are passing a void in a place where a void (*) (wchar_t*, wchar_t*) is expected, so it is correctly indicating that error.
There can be different ways to solve this issue which are mentioned in other answers.
Do I need to use C++ templates?
Of course, you can do that using C++ templates as it follows:
#include<utility>
// ...
template<typename F, typename... A>
void Test2(F &&f, A&&... a)
{
std::forward<F>(f)(std::forward<A>(a)...);
// ...
}
// ...
Test2(Test, L"Hello", L"Testing");
But you don't need them to do what you are trying to do.
#πάνταῥεῖ has already explained why in its answer.
I have a function called generate_all_paths, defined as such:
template <int size>
void generate_all_paths(vector<string> maze[][size], int x, int y) {
....
}
I am trying to call it in my main function as so:
int main() {
string s;
ifstream mazefile("maze.txt");
if (!mazefile) {
cout << "File not found. Please try again." << endl;
}
while (getline(mazefile, s)) {
mazevec.push_back(s);
}
generate_all_paths(mazevec, 0, 1);
return 0;
}
where mazevec is vector<string> mazevec;
But my IDE says that my call to generate_all_paths in main does not match the function definition. I'm a little confused why this is happening. mazevec is a vector string, so shouldn't the parameter data types match up?
The mazevec you are passing to the function is a vector<string>. Your function definition indicates that it expects a 2D vector array. In your function prototype, change it to this:
void generate_all_paths(vector<string> maze, int x, int y);
This should work.
You'll have to pass an array but you have passed a variable that is not an array. So they are treated as two different functions.
Please pass an array of type vector and try again.
please guide me in following regards, i am new to c++. Here i am just practicing Class in C++. While writing an example program, i have just tried to change it a bit and pass function as a parameter to another function.
But i think in due process i am missing some thing....please guide me...Thanks to all
#include
using namespace std;
class my_class{
int i;
int j;
public:
int RealNumber(int a, int b);
void WriteAns(int c);
};
int RealNumber(int a, int b)
{
return a+b;
}
void WriteAns(int c)
{
int i = c;
cout<<"Added numbers are: "<<i<<endl;
}
int main()
{
int j =10, k=20;
int m = 0;
m = RealNumber(j,k);
/*I have tried to pass the function RealNumber in place of m,
while declaring the function as follow:
WriteAns(int *c);
and then i have tried to pass function RealNumber as a parameter
WriteAns(&RealNumber(j,k));
*/
WriteAns(m); //By passing m i get the correct ans
return 0;
}
This is a very minor mistake; you are not trying to pass the actual function as a parameter (as you have implemented) but you are merely trying to pass the reulst of the function.
change &RealNumber(j,k) to RealNumber(j,k) and it should work
You can't use & on an rvalue. The value returned by RealNumber is a temporary, and by extension an rvalue, so &RealNumber(a, b) is invalid. Like the error says, you need an lvalue. Try this instead:
void WriteAns(int* c)
{
cout<<"Added numbers are: "<<*c<<endl;
}
int c = RealNumber(j,k)
WriteAns(&c);
Reference: Value Categories
rvalue (until C++11) / prvalue (since C++11)
A prvalue ("pure" rvalue) is an expression that identifies a temporary
object (or a subobject thereof) or is a value not associated with any
object.
The following expressions are prvalues:
Function call/operator expression if the function's or the operator's return type is not a reference, such as str.substr(1, 2)
or 2+2
If You are trying to pass "function Return value as parameter" , then solution given by Chad should work.
If You are trying to pass "function as parameter" as mentioned above ( pass function as a parameter to another function.), then you need to change signature of WriteAns to receive Function pointer.
void WriteAns(int (*fp)(int,int))
{
int i = (*funcp)(10,20);
cout<<"Added numbers are: "<<i<<endl;
}
WriteAns(&RealNumber(j,k));