-1

This is my code:

HelloApplication::HelloApplication(const Wt::WEnvironment& env)
    : Wt::WApplication(env)
{
    setTitle("Data Interpolation");

    Wt::WText *InitialExplanation = root()->addWidget(std::make_unique<Wt::WText>());

    InitialExplanation->setText("Welcome! This is a page that will allow you to interpolate the value at a point in the domain of your dataset, using other points in your dataset.\n I used Lagrange Interpolation for this calculation.\n Created By Vashist Hegde");

    root()->addWidget(std::make_unique<Wt::WBreak>());

    std::vector<double> xi;
    std::vector<double> yi;
    std::vector<double> xMissing;
    double xM;
   
    auto addDataPoint = [this,xi,yi,lineEdit1,lineEdit2] () mutable{
        
    /// neither of the following push_backs is working.
    /// I get 2 empty vectors in the end.
        xi.push_back(std::move(std::stod(lineEdit1->text())));
        yi.push_back(std::move(std::stod(lineEdit2->text())));

        xi.push_back(3.5);
        yi.push_back(4.5);

    };

I don't get any errors but when I run the program, the vectors are just not getting populated. I get 0 sized vectors. Is this related to my general C++ usage or is it related to Wt (C++ Web toolkit)? If it is related to the general C++ usage, please help me as to what I'm doing wrong.

vashist99
  • 11
  • 2
  • 4
    Your lambda captures `xi` and `yi` by value, not by reference, so the changes it makes to them do not carry over to the variables in `HelloApplication`. You also have omitted the code that shows you _calling_ the lambda instead of just defining it. – Nathan Pierson Oct 25 '22 at 06:00
  • 2
    You're capturing by value and you never call the lambda – Alan Birtles Oct 25 '22 at 06:01
  • Lambda is getting called down the line. – vashist99 Oct 25 '22 at 06:26

1 Answers1

1

Your lambda should capture the variable by ref which has to be to be modified, otherwise the changes will be done to the copy of the captured things. Additionally, you need to invoke lambda for get in action.

auto addDataPoint = [this,&xi,&yi,lineEdit1,lineEdit2] () mutable {        
            // ...... do something
    };

addDataPoint(); // call lambda

alternatively pass them as ref qualified arguments

auto addDataPoint = [this, lineEdit1,lineEdit2] 
    (auto& xi_para, auto &yi_para,) mutable {        
            // ...... do something
    };

addDataPoint(xi, yi); // call lambda with args
````

Const
  • 1,306
  • 1
  • 10
  • 26
  • Lambda is getting called down the line. A new problem I am facing now is that after capturing the vectors by ref, push_back is giving a segmentation fault. Any idea why that might be? Also, I don't think I can use the alternative method you suggested, because the lambda is getting called after a button is clicked and the code to do that does not allow passing arguments. – vashist99 Oct 25 '22 at 06:30
  • 2
    @vashist99 *A new problem I am facing now* -- Which means you should open a new question, this time, giving a [mcve] instead of a code snippet. – PaulMcKenzie Oct 25 '22 at 07:11
  • @vashist99 My guess would be the vectors you've now captured references to have gone out of scope – Alan Birtles Oct 25 '22 at 07:54