我是 Boost 的新手,我正在尝试将它与 C++ 一起使用。我在 Visual Studio 2017 上使用 boost_1_82_0-msvc-14.1-64.exe。我编写了一些读取 COM 端口数据的代码,并且我已尽力修复它,但出于某种原因,无论我做什么,我得到了同样的错误。我收到的第一个错误是“PCH Warning: header stop not at file scope”,我试图用#define no_init_all 修复它,但它似乎不起作用,因为我仍然收到错误。我已经看到使用旧版本的 Boost 会导致这种情况,但我有最新版本的 Boost。所以,我不确定在这里可以做什么。
#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;
}
}
}
如果能帮助我理解我做错了什么以及原因,我将不胜感激。
谢谢。
我测试了你的代码片段并重现了你的问题。 您需要在项目的 perperties->C++->general 中将 boost_1_82_0 文件夹添加为
Additional include directory
。
在解决方案资源管理器中右键单击项目并选择
Properties
.
在项目属性中,选择 C/C++ >
General
.
将 Boost 包含文件夹添加到
Additional Include Directories
属性。例如,如果您在 C:oost_1_75_0 中安装了 Boost,则应将“C:oost_1_75_0”添加到包含目录列表中。
选择链接器 >
General
.
将 Boost 库文件夹添加到
Additional Library Directories
属性。例如,如果您在 C:oost_1_75_0 中安装了 Boost,则应将“C:oost_1_75_0\lib64-msvc-14.2”添加到库目录列表中。
选择
Linker > Input
.
通过在
Additional Dependencies
属性中指定它们的名称,将 Boost 库添加到项目中。