当前位置: 首页 > 知识库问答 >
问题:

使用boost program_选项失败,出现奇怪的编译错误

白彦
2023-03-14

我试图使用Boost的program_options库来构建一个简单的命令行应用程序库,但它失败了,出现了一个非常奇怪的错误。以下是所有有问题的代码(是的,非常简单):

#include <iostream>
#include <stdexcept>
#include <string>
#include <boost/program_options.hpp>
#include "NCDocEngineInterop.h"
#include "NCDocumentEngine.h"
#include "NCConfigurationManager.h"
#include "NCAcquisitions.h"

namespace po = boost::program_options;
using std::cout;
using std::cerr;
using std::endl;
using std::domain_error;
using std::wstring;

void fail_if_empty(const po::variables_map& variables, const char* field) {
    if (!(variables.count(field))) {
        cerr << field << " is required, you must provide a value for it" << endl;
        throw domain_error(field);
    }
}

int main(int ac, char* av[])
{

    const char* nsdk_path_option = "nsdk-path"; // NC_CONFIGURATION::NC_NSDK_PATH
    const char* ocr_path_option = "ocr-path"; // NC_CONFIGURATION::NC_OCR_PATH
    const char* parser_path_option = "parser-path-option"; // NC_CONFIGURATION::NC_PARSER_PATH
    const char* pdf_path = "pdf-path"; // NC_CONFIGURATION::NC_PDF_PATH
    const char* output_directory = "output-directory";
    const char* input_file = "input-file";

    po::options_description description("Runs the NDSK engine at the provided files");
    description.add_options()  
        ("help", "produce help message")
        (nsdk_path_option, po::value<wstring>(), "path to the NSDK")
        (ocr_path_option, po::value<wstring>(), "path to the OCR")
        (parser_path_option, po::value<wstring>(), "path to the parser")
        (pdf_path, po::value<wstring>(),"path to the PDF processor")
        (output_directory, po::value<wstring>(), "path to the folder where the output will be stored")
        (input_file, po::value<wstring>(), "path to the input file")
    ;

    po::variables_map vm;
    po::store(po::parse_command_line(ac, av, description), vm);
    po::notify(vm);

    fail_if_empty(vm, nsdk_path_option);
    fail_if_empty(vm, ocr_path_option);
    fail_if_empty(vm, parser_path_option);
    fail_if_empty(vm, pdf_path);
    fail_if_empty(vm, output_directory);
    fail_if_empty(vm, input_file);

    NCConfigurationManager::ncSetConfiguration(
        NC_NSDK_PATH, 
        vm[nsdk_path_option].as<wstring>() );
    NCConfigurationManager::ncSetConfiguration(
        NC_OCR_PATH,
        vm[ocr_path_option].as<wstring>() );
    NCConfigurationManager::ncSetConfiguration(
        NC_PARSER_PATH,
        vm[parser_path_option].as<wstring>() );
    NCConfigurationManager::ncSetConfiguration(
        NC_PDF_PATH,
        vm[pdf_path].as<wstring>() );

    return 0;
}

发生的编译错误是:

错误C2679:二进制'

错误C2228:左边的'。“失败”必须具有类/struct/union c:\work\digitalbp\projects\releasedev\nsdk\u 4\u 0\dev\source\src\framework\thirdparty\boost\boost\lexical\u cast。hpp 1281 1文档处理器

你可以在这里看到lexical_cast.hpp文件。

我不知道这里发生了什么,也不知道为什么这个代码会出现这个错误。我使用的是boost 1.50。0,我现在无法升级它。我还使用VisualStudio2010来构建它。

下面是输出窗口的输出:

1>c:\work\src\framework\thirdparty\boost\boost\lexical_cast.hpp(1281): error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'const src' (or there is no acceptable conversion)
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(679): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<wchar_t,std::char_traits<wchar_t>>(std::basic_ostream<_Elem,_Traits> &,const char *)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=wchar_t,
1>              _Traits=std::char_traits<wchar_t>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(726): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<wchar_t,std::char_traits<wchar_t>>(std::basic_ostream<_Elem,_Traits> &,char)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=wchar_t,
1>              _Traits=std::char_traits<wchar_t>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(968): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<wchar_t,std::char_traits<wchar_t>,const InputStreamable>(std::basic_ostream<_Elem,_Traits> &&,_Ty)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=wchar_t,
1>              _Traits=std::char_traits<wchar_t>,
1>              InputStreamable=src,
1>              _Ty=src
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(1085): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<wchar_t,std::char_traits<wchar_t>>(std::basic_ostream<_Elem,_Traits> &,const std::error_code &)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=wchar_t,
1>              _Traits=std::char_traits<wchar_t>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(186): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_ostream<_Elem,_Traits> &(__cdecl *)(std::basic_ostream<_Elem,_Traits> &))'
1>          with
1>          [
1>              _Elem=wchar_t,
1>              _Traits=std::char_traits<wchar_t>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(192): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_ios<_Elem,_Traits> &(__cdecl *)(std::basic_ios<_Elem,_Traits> &))'
1>          with
1>          [
1>              _Elem=wchar_t,
1>              _Traits=std::char_traits<wchar_t>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(199): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::ios_base &(__cdecl *)(std::ios_base &))'
1>          with
1>          [
1>              _Elem=wchar_t,
1>              _Traits=std::char_traits<wchar_t>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(206): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::_Bool)'
1>          with
1>          [
1>              _Elem=wchar_t,
1>              _Traits=std::char_traits<wchar_t>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(226): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(short)'
1>          with
1>          [
1>              _Elem=wchar_t,
1>              _Traits=std::char_traits<wchar_t>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(260): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned short)'
1>          with
1>          [
1>              _Elem=wchar_t,
1>              _Traits=std::char_traits<wchar_t>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(280): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(int)'
1>          with
1>          [
1>              _Elem=wchar_t,
1>              _Traits=std::char_traits<wchar_t>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(305): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned int)'
1>          with
1>          [
1>              _Elem=wchar_t,
1>              _Traits=std::char_traits<wchar_t>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(325): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(long)'
1>          with
1>          [
1>              _Elem=wchar_t,
1>              _Traits=std::char_traits<wchar_t>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(345): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned long)'
1>          with
1>          [
1>              _Elem=wchar_t,
1>              _Traits=std::char_traits<wchar_t>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(366): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(__int64)'
1>          with
1>          [
1>              _Elem=wchar_t,
1>              _Traits=std::char_traits<wchar_t>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(386): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned __int64)'
1>          with
1>          [
1>              _Elem=wchar_t,
1>              _Traits=std::char_traits<wchar_t>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(407): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(float)'
1>          with
1>          [
1>              _Elem=wchar_t,
1>              _Traits=std::char_traits<wchar_t>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(427): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(double)'
1>          with
1>          [
1>              _Elem=wchar_t,
1>              _Traits=std::char_traits<wchar_t>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(447): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(long double)'
1>          with
1>          [
1>              _Elem=wchar_t,
1>              _Traits=std::char_traits<wchar_t>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(467): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(const void *)'
1>          with
1>          [
1>              _Elem=wchar_t,
1>              _Traits=std::char_traits<wchar_t>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(487): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_streambuf<_Elem,_Traits> *)'
1>          with
1>          [
1>              _Elem=wchar_t,
1>              _Traits=std::char_traits<wchar_t>
1>          ]
1>          while trying to match the argument list '(std::basic_ostringstream<_Elem,_Traits,_Alloc>, const src)'
1>          with
1>          [
1>              _Elem=wchar_t,
1>              _Traits=std::char_traits<wchar_t>,
1>              _Alloc=std::allocator<wchar_t>
1>          ]
1>          c:\work\src\framework\thirdparty\boost\boost\lexical_cast.hpp(1499) : see reference to function template instantiation 'bool boost::detail::lexical_stream_limited_src<CharT,Traits,RequiresStringbuffer>::shl_input_streamable<const InStreamable>(InputStreamable &)' being compiled
1>          with
1>          [
1>              CharT=char_type,
1>              Traits=traits,
1>              RequiresStringbuffer=true,
1>              InStreamable=src,
1>              InputStreamable=src
1>          ]
1>          c:\work\src\framework\thirdparty\boost\boost\lexical_cast.hpp(1974) : see reference to function template instantiation 'bool boost::detail::lexical_stream_limited_src<CharT,Traits,RequiresStringbuffer>::operator <<<Source>(const InStreamable &)' being compiled
1>          with
1>          [
1>              CharT=char_type,
1>              Traits=traits,
1>              RequiresStringbuffer=true,
1>              Source=src,
1>              InStreamable=src
1>          ]
1>          c:\work\src\framework\thirdparty\boost\boost\lexical_cast.hpp(1921) : while compiling class template member function 'std::wstring boost::detail::lexical_cast_do_cast<Target,Source>::lexical_cast_impl(const Source &)'
1>          with
1>          [
1>              Target=std::wstring,
1>              Source=src
1>          ]
1>          c:\work\src\framework\thirdparty\boost\boost\lexical_cast.hpp(2133) : see reference to class template instantiation 'boost::detail::lexical_cast_do_cast<Target,Source>' being compiled
1>          with
1>          [
1>              Target=std::wstring,
1>              Source=src
1>          ]
1>          c:\work\src\framework\thirdparty\boost\boost\program_options\detail\value_semantic.hpp(89) : see reference to function template instantiation 'Target boost::lexical_cast<T,std::basic_string<_Elem,_Traits,_Ax>>(const Source &)' being compiled
1>          with
1>          [
1>              Target=std::wstring,
1>              T=std::wstring,
1>              _Elem=char,
1>              _Traits=std::char_traits<char>,
1>              _Ax=std::allocator<char>,
1>              Source=std::basic_string<char,std::char_traits<char>,std::allocator<char>>
1>          ]
1>          c:\work\src\framework\thirdparty\boost\boost\program_options\detail\value_semantic.hpp(170) : see reference to function template instantiation 'void boost::program_options::validate<T,char>(boost::any &,const std::vector<_Ty> &,T *,long)' being compiled
1>          with
1>          [
1>              T=std::wstring,
1>              _Ty=std::string
1>          ]
1>          c:\work\src\framework\thirdparty\boost\boost\program_options\detail\value_semantic.hpp(163) : while compiling class template member function 'void boost::program_options::typed_value<T,charT>::xparse(boost::any &,const std::vector<_Ty> &) const'
1>          with
1>          [
1>              T=std::wstring,
1>              charT=char,
1>              _Ty=std::string
1>          ]
1>          c:\work\src\samples\cpp\documentprocessor\documentprocessor\main.cpp(37) : see reference to class template instantiation 'boost::program_options::typed_value<T,charT>' being compiled
1>          with
1>          [
1>              T=std::wstring,
1>              charT=char
1>          ]
1>c:\work\src\framework\thirdparty\boost\boost\lexical_cast.hpp(1281): error C2228: left of '.fail' must have class/struct/union
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

共有1个答案

商燕七
2023-03-14

问题其实就在这里:

po::store(po::parse_command_line(ac, av, description), vm);

parse_command_line实例化一个basic_command_line_parser

如果您仔细想想,这实际上是有道理的,因为命令行选项总是以普通的旧“c”字符串的形式出现。即使它们是mbcs编码的,您也有责任使用正确的语言环境执行转换。不幸的是,这是必要的,因为MBCS的解释是特定于语言环境的。

我查看了一下是否可以创建选项描述的wchar_t版本(如果您想从wmain()使用它,但事实并非如此)<代码>选项\u说明包含硬编码的std::strings。

字节字符串引用

 类似资料:
  • 问题内容: 让我们使用Eclipse Mars.2软件包中的ECJ编译器编译以下代码: 编译命令如下: 成功编译后,让我们使用来检查生成的类文件。最有趣的是为lambda 生成的合成方法: 看到这个条目我感到很惊讶。JVM规范涵盖了 LocalVariableTypeTable属性,并说: 该索引处的条目必须包含表示字段签名的结构(第4.4.7节),该签名对源程序中的局部变量的类型进行编码(第4.

  • 我有这个方法: TableField很简单,就像: 但我看到了这个编译错误: 不兼容的类型。必需的集合,但已将“映射”推断为流:不存在类型为变量R的实例,因此流符合集合

  • 我在打印报告时出现了一个奇怪的错误。

  • 问题内容: 我一直在使用PyCharm在PyQt中调试我的GUI。到目前为止,这确实非常成功,直到我在尝试调试gui时遇到一个奇怪的错误。我已经在脚本的开始以及各个点设置了一个断点,但是程序没有机会到达这一点。我也尝试删除所有断点并运行调试,但得到相同的结果。完整的回溯是: 有谁知道导致此错误的原因以及如何解决?当我正常运行代码(无需调试)时,不会遇到任何这些错误。 问题答案: 我遇到了同样的问题

  • 问题内容: 我一般对Python和编程都不熟悉,所以如果我遗漏了一些明显的东西,请提前道歉。我正在尝试绘制图形并标记轴,但是每次尝试标记y轴时都会引发异常。我在下面的新脚本中编写了代码,以确保问题不是来自模块中的其他地方。我正在使用Python 3.4。 每次,我在最后一行收到错误“ TypeError:’str’对象不可调用”。如果我将y更改为x,一切都很好。如果将x更改为ay,则会收到相同的错

  • 我有一个数据帧,我试图在条形图中绘制,但我面临一个奇怪的错误。 print语句给出: 然后是绘图代码:给出以下错误: ---------------------------------------------------------------------------KeyError回溯(最近一次调用上次)~/opt/anaconda3/lib/python3。8/现场包/熊猫/核心/索引/基础。