Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use an argument like std::vector<std::complex<double>> in boost::asio::buffer

Tags:

c++

c++11

boost

I am trying to create an UDP communication between two hosts using boost library, but instead of sending/receiving a string i want to send a std::vector<std::complex<double> structure.

To be more specific, i have a greater function that returns me a buffer of std::vector<std::complex<double> type. By using .front() i can see the values from the buffer. The catch is that i want to use buffer.front() as an argument to a sender function, namely "auto sent = socket.send_to(boost::asio::buffer(buffer.front()), remote_endpoint, 0, err);"

I attached a part of the code of interest if it helps:

void Sender(std::vector<std::complex<double>> in) {

    boost::asio::io_service io_service;
    udp::socket socket(io_service);
    udp::endpoint remote_endpoint = udp::endpoint(address::from_string(IPADDRESS), UDP_PORT);
    socket.open(udp::v4());

    //std::cout<<"The message is:"<< in<<std::endl;

    boost::system::error_code err;
    auto sent = socket.send_to(boost::asio::buffer(in), remote_endpoint, 0, err);
    socket.close();
    std::cout << "Sent Payload --- " << sent << "\n";
}


        int main()
    {.....
     .....
     .....
     for (int i = 0; i < 20; ++i) {
       std::this_thread::sleep_for(std::chrono::milliseconds(1000));
       Sender(buff.front());
    }

My problem is in the Sender argument what should i have instead ofstd::vector<std::complex<double> because is not working, i get an error of conversion, and the next question is if it is posible of doing this, i mean sending this type of data.

I also attached the error that return me if i try to compile:

error: could not convert ‘input.std::vector<std::complex<double> >::front()’ from ‘__gnu_cxx::__alloc_traits<std::allocator<std::complex<double> > >::value_type {aka std::complex<double>}’ to ‘std::vector<std::complex<double> >’
    Sender(input.front());

Thank you!

like image 233
Victor.petrescu Avatar asked Mar 28 '26 13:03

Victor.petrescu


1 Answers

Looking at your error message again:

error: could not convert ‘input.std::vector<std::complex<double> >::front()’ from ‘__gnu_cxx::__alloc_traits<std::allocator<std::complex<double> > >::value_type {aka std::complex<double>}’ to ‘std::vector<std::complex<double> >’

Sender(input.front());

If we reword it slightly as:

error: could not convert ‘input.std::vector<std::complex<double> >::front()’ from std::complex<double>’ to ‘std::vector<std::complex<double> >’

Sender(input.front());

It becomes a bit more obvious what the problem is. input.front() just returns (a reference to) the first element of the vector, but Sender wants the whole vector. You want:

Sender(input);

Aside: I would change the argument to Sender to be:

void Sender(const std::vector<std::complex<double>>& in)

This way you don't need to make a copy of the vector - you just use a (constant) reference to the original vector. If you are going to be sending this over UDP, copying the vector is probably pretty insignificant - but it's a useful habit to get into.

like image 171
Martin Bonner supports Monica Avatar answered Mar 30 '26 03:03

Martin Bonner supports Monica



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!