Class Instantiated from here error c++

I am trying to create a class that will read and parse data from a Serial port, however I keep getting an instantiated error and I don't know why. The class takes in a serial port and its io_service. I am using boost. I am getting a ton of errors, but I think its because its cumulative (I think, I am not sure if that is correct). Here is the first one:

In file included from /usr/include/boost/bind.hpp:22:0, from ../Sources/Magnetic Compensator Core.cpp:17:

/usr/include/boost/bind/bind.hpp: In instantiation of 'boost::_bi::result_traits&, const boost::system::error_code&, unsigned int)>': /usr/include/boost/bind/bind_template.hpp:15:48: instantiated from 'boost::_bi::bind_t&, const boost::system::error_code&, unsigned int), boost::_bi::list4, boost::reference_wrapper >, boost::arg<1>, boost::arg<2> > >'

Here is the code for the class:

class mag_serial
{
bool data_available;
boost::asio::serial_port& ser_port;
boost::asio::deadline_timer timeout;
char my_buffer[1];
std::string str;
std::string st;


void read_callback(bool& data_available, boost::asio::deadline_timer& timeout, const boost::system::error_code& error, std::size_t bytes_transferred)
        {

            data_available = true;
            if(str.length() > 1)
            {
            if (!(str.at(str.length() - 1) == temp))//&str.at(str.length() - 1) != "#")
            {
                str.append(my_buffer,bytes_transferred);
                if(str.at(str.length() - 1) == quit)
                {
                    cout << "I am quitting";

                            Stop();
                            ser_port.cancel();
                            ser_port.close();
                            return;
                }

                i++;
            }

            else if (str.at(str.length() - 1) == temp)
            {
                st = str;//.substr(1, str.size());

                // Processing Functions
            }
            }
            else
            {
                str.append(my_buffer,bytes_transferred);
                if(str.at(0) == quit)
                {
                    cout << "I am quitting";
                    Stop();
                            ser_port.cancel();
                            ser_port.close();
                            return;
                }
            }
            ser_port.async_read_some(boost::asio::buffer(my_buffer),
                boost::bind(&mag_serial::read_callback, boost::ref(data_available),
                        boost::ref(timeout),boost::asio::placeholders::error(),
                        boost::asio::placeholders::bytes_transferred()));
            data_available = true;
        }

        void wait_callback(boost::asio::serial_port& ser_port, const boost::system::error_code& error)
        {
            if (error)
            {
                // Data was read and this timeout was cancelled

                return;
            }

        }




public:
mag_serial(boost::asio::serial_port& ser_port, boost::asio::io_service& io_svc): ser_port(ser_port), timeout(ser_port.get_io_service()){}



void read_mag_serial_thread()
{
    bool data_available = false;

    ser_port.async_read_some(boost::asio::buffer(my_buffer),
            boost::bind(&mag_serial::read_callback, boost::ref(data_available),
                    boost::ref(timeout),boost::asio::placeholders::error(),
                    boost::asio::placeholders::bytes_transferred()));
    timeout.expires_from_now(boost::posix_time::seconds(1));
    timeout.async_wait(boost::bind(&mag_serial::wait_callback, boost::ref(ser_port),boost::asio::placeholders::error()));

    io_svc.run();

    if(!data_available)
    {
        ser_port.close();
        cout << "ser_port was closed";
    }
}

};

This won't compile

ser_port.async_read_some(boost::asio::buffer(my_buffer),
                boost::bind(&mag_serial::read_callback, boost::ref(data_available),
                        boost::ref(timeout),boost::asio::placeholders::error(),
                        boost::asio::placeholders::bytes_transferred()));

the member function mag_serial::read_callback needs an instance to bind to.

ser_port.async_read_some(
    boost::asio::buffer(my_buffer),
    boost::bind(
        &mag_serial::read_callback,
        this,
        boost::ref(data_available),
        boost::ref(timeout),
        boost::asio::placeholders::error,
        boost::asio::placeholders::bytes_transferred
    )
);

Here's a coliru, I didn't attempt to fix the formatting.

链接地址: http://www.djcxy.com/p/60650.html

上一篇: 用C ++ Boost实现XML序列化:如何引用基类?

下一篇: 类从这里实例化错误c ++