在名称空间的类中对枚举使用boost :: program_options

蒂姆·安德森(Tim Anderson)

我正在使用boost :: program_options来解析我的程序的命令行,并且在尝试将值读入也是名称空间的类中的公共枚举时遇到了麻烦。

细节:

Boost 1.44.0
g++ 4.4.7

我尝试遵循Boost Custom Validator for Enum中阐明的过程但对我不起作用。

参数.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>
     }
}

Parmae​​ters.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&)'

我尝试将运算符>>放入main并得到相同的错误。

我现在已经花了几天时间了,所以我不打算从这里去。如果有人有任何想法,将不胜感激。

贾罗德42

用您的朋友声明,您声明

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() {}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在名称空间中使用boost :: program_options :: validate自己的类

boost program_options 中的链接错误

boost :: program_options位置选项

boost program_options如何工作?

用boost :: optional增强program_options

RAII与boost boost :: program_options和options_description

boost program_options中vector <string>选项的拆分值

在xubuntu中对boost :: program_options的未定义引用

boost :: program_options bool_switch使用了多次

使用 boost::program_options 的不带破折号的参数

boost :: program_options配置文件格式

Boost Program_Options抛出“字符转换失败”

向量值boost :: program_options的默认值

boost :: program_options和位置参数的问题

Boost Program_Options 如何处理数组向量?

使用 boost 的 program_options 时,如何确保声明在 C++ 中具有存储类或类型说明符?

boost :: program_options :: options_description对于简化的命令名不起作用

如何使用boost :: posix_time :: ptime获得boost :: program_options?

使用后如何使用boost program_options在ARGV中使用/删除选项

在Boost program_options中的配置文件中处理无值选项

使用boost :: program_options禁止对无符号值使用负参数

如何在boost :: program_options :: variable_map中存储数据?

使用boost :: program_options解析文本文件是一个好主意吗?

在Windows中使用boost :: program_options从命令行参数读取Unicode字符

使用boost :: {program_options,property_tree}读取/写入信息

为什么boost :: any在boost :: program_options中表现出未定义的行为?

c ++ / boost program_options一个选项禁用其他

如何避免在boost :: program_options中将位置选项指定为“常规”选项?

当指定了意外的位置参数时,boost :: program_options不报告错误