I’m new to Boost and I’m trying to use it with C++. I’m using boost_1_82_0-msvc-14.1-64.exe on Visual Studio 2017. I’ve written some code that reads COM port data and I’ve tried my best to fix it, but for some reason, no matter what I do, I get the same errors. The first error I received was ‘PCH Warning: header stop not at file scope,’ which I tried to fix with #define no_init_all, but it doesn’t seem to work as I’m still getting the error. I’ve seen that having an older version of Boost can lead to this, but I have the latest version of Boost. So, I’m unsure what can be done here.
- errors include:
- E0135 namespace "boost::asio" has no member "io_context"
- E0135 namespace "boost::asio" has no member "serial_port"
- E0135 namespace "boost::asio" has no member "read_until"
- E0135 namespace "boost::asio" has no member "dynamic_buffer"
#if (_MSC_VER >= 1915)
#define no_init_all deprecated
#endif
#include <iostream>
#include <boost/asio.hpp>
#include <string>
#include <boost/asio/io_service.hpp>
struct SensorData {
float heading;
float magneticNorth;
float magneticSouth;
};
SensorData readSerialData() {
boost::asio::io_context io;
boost::asio::serial_port port(io, "COM3");
port.set_option(boost::asio::serial_port_base::baud_rate(115200));
SensorData sensorData;
std::string sensorDataStr;
std::size_t headingPos, magNorthPos, magSouthPos;
std::string headingStr, magNorthStr, magSouthStr;
while (true)
{
boost::asio::read_until(port, boost::asio::dynamic_buffer(sensorDataStr), 'n'); // Read until a newline character is received
headingPos = sensorDataStr.find("Heading:"); // Find the position of "Heading:" in the string
if (headingPos != std::string::npos) {
headingStr = sensorDataStr.substr(headingPos + 9); // Extract the heading value as a string
sensorData.heading = std::stof(headingStr); // Convert the heading string to a float
}
magNorthPos = sensorDataStr.find("Magnetic North:"); // Find the position of "Magnetic North:" in the string
if (magNorthPos != std::string::npos) {
magNorthStr = sensorDataStr.substr(magNorthPos + 16); // Extract the magnetic north value as a string
sensorData.magneticNorth = std::stof(magNorthStr); // Convert the magnetic north string to a float
}
magSouthPos = sensorDataStr.find("Magnetic South:"); // Find the position of "Magnetic South:" in the string
if (magSouthPos != std::string::npos) {
magSouthStr = sensorDataStr.substr(magSouthPos + 16); // Extract the magnetic south value as a string
sensorData.magneticSouth = std::stof(magSouthStr); // Convert the magnetic south string to a float
}
if (headingPos != std::string::npos && magNorthPos != std::string::npos && magSouthPos != std::string::npos) {
// All data has been received, so return the SensorData struct
return sensorData;
}
}
}
I would appreciate help in understanding what I’ve done wrong and why.
Thanks.
2
Answers
So I've been playing around, and it seems it works in Visual Studio 2019, not 2017, and only in x86. This is very strange. I'm thinking that either there might be a bug or another version I must have installed a long time ago and forgotten about.
Right-click on the project in the Solution Explorer and select
Properties
.In the project properties, select C/C++ >
General
.Add the Boost include folder to the
Additional Include Directories
property. For example, if you installed Boost in C:boost_1_75_0, you would add "C:boost_1_75_0" to the include directories list.Select Linker >
General
.Add the Boost library folder to the
Additional Library Directories
property. For example, if you installed Boost in C:boost_1_75_0, you would add "C:boost_1_75_0lib64-msvc-14.2" to the library directories list.Select
Linker > Input
.Add the Boost libraries to the project by specifying their names in the
Additional Dependencies
property.