1

I am continuing my studies of C++ and I came across lambdas. I am following a simple tutorial just to get a feel for the sintax, but the following code is failing:

#include "stdafx.h" #include <string> #include <iostream> using namespace std; void runDivide(double (*divide)(double a, double b)) { auto rval = divide(a , b); cout << rval << endl; } int main() { auto funcDiv = [](double value1, double value2) -> double{ if (value2 == 0.0) { return 0; } return value1 / value2; }; runDivide(funcDiv); system("pause"); return 0; } 

This is giving me a "Identifier 'a' is undefined" and "Identifier 'b' is undefined". Although I am copying it verbatin from the tutorial, maybe I'm missing something?

Thanks in advance for the help!

Michael

4
  • 1
    You forgot to pass the function arguments. Commented Feb 13, 2017 at 19:25
  • 1
    There's no variables a and b in runDivide(). Commented Feb 13, 2017 at 19:28
  • Instead of copying try to understand what the code does. If you understood, then you'd understand that a and b aren't defined anywhere. They're simply in the type of the function pointer. Commented Feb 13, 2017 at 19:28
  • Thanks for this. I thought I had understood it but I can see now that I had developed "tunnel vision" and missed this ! Commented Feb 14, 2017 at 19:09

4 Answers 4

1

runDivide() uses the variables a and b, but never declares them or gives them values. They should be arguments to runDivide, not part of the declaration of the function argument (you don't need to give names to those arguments, just declare the types).

#include "stdafx.h" #include <string> #include <iostream> using namespace std; void runDivide(double (*divide)(double, double), double a, double b) { auto rval = divide(a , b); cout << rval << endl; } int main() { auto funcDiv = [](double value1, double value2) -> double{ if (value2 == 0.0) { return 0; } return value1 / value2; }; runDivide(funcDiv, 30.0, 12.3); system("pause"); return 0; } 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! It seems obvious now (hindsight and all). I guess because I'm new to this aspect of C++, I got confused with the difference between the type of the function pointer and the paremeter names themselves!
1

You are allowed to name parameters in a function pointer, like here:

double (*divide)(double a, double b) 

The name a and b are names of the two doubles that the function that the function pointer divide points to.

However, such names have no impact on the behavior of your code in C++.

double (*divide)(double, double) 

This has exactly the same meaning.

divide is a function pointer that points at a function that takes two doubles and returns one double.

void runDivide(double (*divide)(double, double)) { auto rval = divide(a , b); cout << rval << endl; } 

this makes it far more obvious that a and b have no meaning at this point.

You either need to pass double a and double b to runDivide, or have them visible in the scope of runDivide.

This "the function parameter names have no effect" is true both with function pointers and with function declarations (but not with function definitions or lambda definitions).

To fix this change

runDivide(funcDiv); 

to

runDivide(funcDiv,1.0, 2.0); 

and

void runDivide(double (*divide)(double, double)) { auto rval = divide(a , b); cout << rval << endl; } 

to

void runDivide(double (*divide)(double, double), double a, double b) { auto rval = divide(a , b); cout << rval << endl; } 

Alternatively, simply add global variables double a and double b before runDivide, and set them to something reasonable. I would consider that a bad idea, as global variables are a mess.

Comments

0

The only argument to runDivide is divide. The a and b are not used and can be left out. They are just place holders for the arguments of the passed in function.

Comments

0

If I remember correctly, you cannot normally make a call from one function to another function's arguments. Due to the fact they can be accessed only within the specific function, you must declare these arguments/variables globally. Next thing you forgot to declare them in further part of the code.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.