C++ 无法解析变量 BOOST_FOREACH

标签 c++ boost clion

我尝试使用 Boost 库,我复制了整个 boost 文件夹,除了 docs、libs、more、status、tools 文件夹。

当我尝试使用下面的代码块时,我的编译器无法识别两件事。

vector<string>* read(string & filename)
{


 // populate tree structure pt
    using boost::property_tree::ptree;
    ptree pt;
    read_xml(filename, pt);
    ptree tree;

vector<string> *ans = new vector<string>();

BOOST_FOREACH( ptree::value_type &v, pt.get_child("computer"))
{
    string name = v.first.get<string>("name");
    string OS = v.first.get<string>("OS");

    ans->push_back(name);
    ans->push_back(OS);
}

return ans;
}
  1. “BOOST_FOREACH”未在此范围内声明
  2. 无法解析结构成员“value_type”

我知道以下包含行应该足够了:

#include <iostream>
#include <vector>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>

如果您需要更多信息,请询问。 时间差

编辑

添加 include foreach.hpp 后,我得到:

this issue

最佳答案

I know the following include lines should be enough:

显然他们不是。添加

#include <boost/foreach.hpp>

固定代码:

Live On Coliru

#include <iostream>
#include <vector>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/foreach.hpp>

std::vector<std::string> read(std::string & filename)
{
    // populate tree structure pt
    using boost::property_tree::ptree;

    ptree pt;
    read_xml(filename, pt);
    ptree tree;

    std::vector<std::string> ans;

    BOOST_FOREACH(ptree::value_type &v, pt.get_child("computer"))
    {
        std::string name = v.second.get<std::string>("name");
        std::string OS   = v.second.get<std::string>("OS");

        ans.push_back(name);
        ans.push_back(OS);
    }

    return ans;
}

int main()
{
}

关于C++ 无法解析变量 BOOST_FOREACH,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33619192/

相关文章:

c++ - std::map 成员函数指针?

c++ - 对于非常大的非负 n 和 m,n %= m 能否返回负值?

c++ - __syncthreads() 之后的 CUDA 竞赛检查危险

c++ - 使用 boost::pfr 获取字段名称

c++ - V8,将文本 append 到数组的最后一个元素

c++ - Boost 程序选项添加选项语法

c++ - 如何在我的 Mac 上安装 boost?

ios - kotlin native 未知主机目标

Clion 无法解析 __int128_t 但编译器另有说明

c++ - CLion 发现错误的函数签名