简体   繁体   中英

Problems compiling boost signal2

Why does this simple example not compile, and how can I get around the problem?

#include <iostream>
#include <boost/signals2/signal.hpp>

struct HelloWorld {
    HelloWorld() {
        i = 0;
    }

    void operator()() {
        std::cout << "I is: " << i++ << std::endl;
    }

    void setup () {
        sig.connect(this);
    }

    void run () {
        sig();
    }

    boost::signals2::signal<void ()> sig;

    private:
        int i;
};

int main()
{
  HelloWorld hello;
  hello.setup();
  hello.run();
  hello.run();
  hello.run();

  return 0;
};

You are trying to connect to a pointer, which isn't possible. Instead you need to connect to a reference to your object:

void setup () {
    sig.connect(boost::ref(*this));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM