【问题标题】:Using boost::program_options with enum in class in namespace在命名空间的类中使用 boost::program_options 和枚举
【发布时间】:2014-06-19 15:12:48
【问题描述】:

我正在使用 boost::program_options 为我的程序解析命令行,但在尝试将值读入同样位于命名空间中的类中的公共枚举时遇到了麻烦。

具体说明:

Boost 1.44.0
g++ 4.4.7

我尝试按照Boost Custom Validator for Enum 中说明的流程进行操作,但它不适合我。

Parameters.h

#include <istream>

namespace SA
{
    class Parameters
    {
    public:
        enum Algorithm
        {
            ALGORITHM_1,
            ALGORITHM_2,
            ALGORITHM_3,
            ALGORITHM_4
        };

        friend istream& operator>> (istream &in, Parameters::Algorithm &algorithm);

        Algorithm mAlgorithm;

        <More Parameters>
     }
}

参数.cpp

#include <boost/algorithm/string.hpp>

using namespace SA;

istream& operator>> (istream &in, Parameters::Algorithm &algorithm)
{
    string token;
    in >> token;

    boost::to_upper (token);

    if (token == "ALGORITHM_1")
    {
        algorithm = ALGORITHM_1;
    }
    else if (token == "ALGORITHM_2")
    {
        algorithm = ALGORITHM_2;
    }
    else if (token == "ALGORITHM_3")
    {
        algorithm = ALGORITHM_3;
    }
    else if (token == "ALGORITHM_4")
    {
        algorithm = ALGORITHM_4;
    }
    else
    {
        throw boost::program_options::validation_error ("Invalid Algorithm");
    }

    return in;
}

main.cpp

#include <boost/program_options.hpp>

using namespace SA;

int main (int argc, char **argv)
{
    po::options_description options ("Test: [options] <data file>\n    Where options are:");
    options.add_options ()
        ("algorithm", po::value<Parameters::Algorithm>(&Parameters::mAlgorithm)->default_value (Parameters::ALGORITHM_3), "Algorithm");
    <More options>

    <...>
}

编译时出现以下错误:

main.o: In function 'bool boost::detail::lexical_stream_limited_src<char, std::basic_streambuf<char, std::char_traits<char> >, std::char_traits<char> >::operator>><SA::Parameters::Algorithm>(SA::Parameters::Algorithm&)':
/usr/include/boost/lexical_cast.hpp:785: undefined reference to 'SA::operator>>(std::basic_istream<car, std:char_traits<char> >&, SA::Parameters::Algorithm&)'

我尝试将 operator>> 放在 main 中,得到了同样的错误。

我现在已经在这上面花了几天时间,但我不知道该去哪里。如果有人有任何想法,将不胜感激。

【问题讨论】:

  • 尝试在namespace SA 中定义你的operator&gt;&gt;

标签: c++ boost-program-options


【解决方案1】:

用你的朋友声明,你声明

namespace SA {
    istream& operator>> (istream &in, Parameters::Algorithm &algorithm);
}

还有你在全局命名空间中的实现:

istream& operator>> (istream &in, Parameters::Algorithm &algorithm);

将你的实现移到namespace SA中。

供您参考:

namespace SA { void foo(); }

using namespace SA;

void foo() {} // implement ::foo and not SA::foo()

你必须使用

namespace SA { void foo() {} }

void SA::foo() {}

【讨论】:

  • 不幸的是,我的代码所在的计算机不在 Internet 上,所以我不得不在另一台计算机上输入此代码并忘记输入我有一个 using namespace SA。我已经编辑了我的原始帖子以反映它一直都在那里。
  • 我的回答仍然有效。我已经进行了编辑以突出显示一些要点。
  • 好的。我没有意识到他们是不同的。现在可以了!谢谢!
猜你喜欢
  • 2019-01-18
  • 1970-01-01
  • 2017-03-11
  • 2012-09-08
  • 1970-01-01
  • 2011-10-28
  • 2011-09-12
  • 2014-10-25
  • 2019-10-23
相关资源
最近更新 更多