Boost 程序选项教程示例中的“<<" operator in cout with <vector<string>>”错误

问题描述 投票:0回答:1

我正在使用 Vstual Studio 2022 并使用 c++ 14

if (vm.count("include-path"))
{
    cout << "Include paths are: "
        << vm["include-path"].as< vector<string> >() << "\n";
}

我第二个有错误<< operator. The error says thats there is no "<<" operator matching the operands.

自从我一步步遵循 boost_program_options 教程以来,我就没有什么可以尝试的了。

编辑: 我尝试自己“解决”定义运算符的问题:

template <class T>
ostream& operator <<(ostream& os, const vector<T>& v) {
    os << "[";
    for (auto it = v.begin(); it != v.end(); ++it)
        os << *it << " ";
    os << "]";
    return os;
}
windows boost visual-studio-2022
1个回答
0
投票

回复:

我尝试自己“解决”定义运算符的问题:

它需要在通过参数依赖查找关联的命名空间中定义:https://en.cppreference.com/w/cpp/language/adl

在这种情况下,您不能拥有除

std
之外的其他命名空间,因为它声明了
std::vector
std::string
(并且
std::char_traits<char>
std::allocator
是完整的)。

© www.soinside.com 2019 - 2024. All rights reserved.