Project

General

Profile

WCheckBox help * NEWBIE*

Added by Giovanni Marchetto almost 5 years ago

Hi all!

I've copied the following code from hello World! and i've added only a checkbox for test.

#include <Wt/WApplication.h>
#include <Wt/WBreak.h>
#include <Wt/WContainerWidget.h>
#include <Wt/WLineEdit.h>
#include <Wt/WPushButton.h>
#include <Wt/WText.h>
#include <Wt/WCheckBox.h>
#include <iostream>

class HelloApplication : public Wt::WApplication
{
public:
    HelloApplication(const Wt::WEnvironment& env);

private:
    Wt::WLineEdit *nameEdit_;
    Wt::WText *greeting_;
    Wt::WCheckBox *test_;


};

HelloApplication::HelloApplication(const Wt::WEnvironment& env)
    : Wt::WApplication(env)
{
    setTitle("Hello world");

    root()->addWidget(std::make_unique<Wt::WText>("Your name, please? "));

    nameEdit_ = root()->addWidget(std::make_unique<Wt::WLineEdit>());
    Wt::WPushButton *button = root()->addWidget(std::make_unique<Wt::WPushButton>("Greet me."));
    root()->addWidget(std::make_unique<Wt::WBreak>());
    Wt::WCheckBox *test_ = root()->addWidget(std::make_unique<Wt::WCheckBox>());



    root()->addWidget(std::make_unique<Wt::WBreak>());
    greeting_ = root()->addWidget(std::make_unique<Wt::WText>());
    auto greet = [this]{
      greeting_->setText("Hello there, " + nameEdit_->text());
      std::cout << test_->isChecked() << std::endl;
    };
    button->clicked().connect(greet);
}

int main(int argc, char **argv)
{
    return Wt::WRun(argc, argv, [](const Wt::WEnvironment& env) {
      return std::make_unique<HelloApplication>(env);
    });
}

When i try to compile the code, an error occur on lambda function:

hello.cc: In lambda function:                                                                                                                                                                     
hello.cc:41:20: error: ‘test_’ is not captured                                                                                                                                                    
       std::cout << test_->isChecked() << std::endl;                                                                                                                                              
                    ^~~~~                                                                                                                                                                         
hello.cc:39:23: note: the lambda has no capture-default                                                                                                                                           
     auto greet = [this]{                                                                                                                                                                         
                       ^                                                                                                                                                                          
hello.cc:33:20: note: ‘Wt::WCheckBox* test_’ declared here                                                                                                                                        
     Wt::WCheckBox *test_ = root()->addWidget(std::make_unique<Wt::WCheckBox>());                                                                                                                 
                    ^~~~~            

Thanks for patience.


Replies (2)

RE: WCheckBox help * NEWBIE* - Added by Roel Standaert almost 5 years ago

The lambda function only captures this, which is the HelloApplication, so it can only access members of the HelloApplication, and not local variables, like your test_.

The reason why your test_ is a local variable, and not a member of HelloApplication, is because you are declaring the type again.

If you change this line:

Wt::WCheckBox *test_ = root()->addWidget(std::make_unique<Wt::WCheckBox>());

to this:

test_ = root()->addWidget(std::make_unique<Wt::WCheckBox>());

it should work.

RE: WCheckBox help * NEWBIE* - Added by Giovanni Marchetto almost 5 years ago

Thanks for quick and clear reply, and sorry for this stupid question.

I've see now (with your explanation) , the giant mistake that i had made.

Now it's compile.

Have a nice day and thanks again!

    (1-2/2)