PPP2 Chapter 15 - Compiling exp() approximation Function Graphing Example - Help Needed

Until a bit recently, I was stuck on compiling the example code for graphing the approximation of the exp() function, expe(), on Chapter 15. Dr. Stroustrup used a lambda function as the first argument to Function's constructor, but my compiler doesn't like me doing that. Visual Studio 2017. I tried the MSVC-v141 toolset, the Clang/C2 toolset and the LLVM-vs2014 toolset all with the same result. I want to compile it with Clang on the command-line, but I don't know the correct command-line switches to link the FLTK libraries and specify the Include Paths to it, as well as the switches for telling it where to find .h and .cpp files for Dr. Stroustrup's GUI interface library for the book. I couldn't do it at all on the normal Windows Command Prompt, so I tried it on Bash on Windows (WSL); I still couldn't do it successfully, but at least it now it seems like I just need to tell it correctly where the GUI interface library is (would be good if someone told me how to specify the Include Paths to the FLTK root directory as well, such that it can find the headers, as well as how to provide the path to the Library directory for FLTK so it can find the library object files).

What other IDE can I use on Windows 10 (with the Creator's Update installed, latest build and everything) that works well with LLVM, one that I can successfully compile and link with that code?

Note: Here's the error message from my compiler:

1>main.cpp(66,12): error : no matching constructor for initialization of 'Graph_lib::Function'
1> Function e{ [n](double x) { return expe(x, n); },
1> ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1>./../../Graph.h(167,3): note: candidate constructor not viable: no known conversion from '(lambda at main.cpp:66:15)' to 'Fct *' (aka 'double (*)(double)') for 1st argument
1> Function(Fct f, double r1, double r2, Point orig, int count = 100, double xscale = 25, double yscale = 25);
1> ^
1>./../../Graph.h(165,9): note: candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 7 were provided
1> struct Function : Shape {
1> ^
1>90 warnings and 1 error generated.


The line of code that generated the error was the first of these two lines:
1
2
Function e{ [n](double x) { return expe(x, n); },
			r_min, r_max, orig, 200, x_scale, y_scale };


I'm not sure how Dr. Stroustrup and others got that code to compile because apparently, lambda functions that use captures can't be passed as arguments to function parameters expecting a function pointer. And there's also no clean solution, it seems. Could someone help me solve this?

I later found one workaround that at least seems to be working (not sure how to check in my debugger because I'm not sure what values n is meant to have): I defined a static variable s_n and a function
1
2
3
4
5
double expe2(double x)
{
	return expe(x, n_s);
	n_s = 0;
}


In main I do this in the loop where the lambda function was originally:
1
2
3
4
5
6
7
8
9
10
11
12
for (int n = 0; n < 50; ++n)
{
	ostringstream ss;
	ss << "exp approximation; n==" << n;
	win.set_label(ss.str());
	// get next approximation:
	Function e{ expe2, r_min, r_max, orig, 200, x_scale, y_scale };
	win.attach(e);
	win.wait_for_button();
	win.detach(e);
	n_s = n;
}


Note the last line in the loop body.

Is this a good idea, though? What would be the best way to solve this? Could someone help me with creating a function pointer to a function lambda that captures?
Last edited on
Topic archived. No new replies allowed.