无法运行以下代码

时间:2014-07-15 16:51:35

标签: c++

我正在尝试精益平均C ++选项解析器,我尝试了示例

#include <iostream>
 #include "optionparser.h"

 enum  optionIndex { UNKNOWN, HELP, PLUS };

 const option::Descriptor usage[] =
 {
  {UNKNOWN, 0,"" , ""    ,option::Arg::None, "USAGE: example [options]\n\n"
                                             "Options:" },
  {HELP,    0,"" , "help",option::Arg::None, "  --help  \tPrint usage and exit." },
  {PLUS,    0,"p", "plus",option::Arg::None, "  --plus, -p  \tIncrement count." },
  {UNKNOWN, 0,"" ,  ""   ,option::Arg::None, "\nExamples:\n"
                                             "  example --unknown -- --this_is_no_option\n"
                                             "  example -unk --plus -ppp file1 file2\n" },
  {0,0,0,0,0,0}
 };

 int main(int argc, char* argv[])
 {
   argc-=(argc>0); argv+=(argc>0);              // skip program name argv[0] if present
   option::Stats  stats(usage, argc, argv);    
   option::Option options[stats.options_max], buffer[stats.buffer_max]; //------------Error Comes here-----------------------
   option::Parser parse(usage, argc, argv, options, buffer);

   if (parse.error())
     return 1;

   if (options[HELP] || argc == 0) {
     option::printUsage(std::cout, usage);
     return 0;
   }

   std::cout << "--plus count: " <<
     options[PLUS].count() << "\n";

   for (option::Option* opt = options[UNKNOWN]; opt; opt = opt->next())
     std::cout << "Unknown option: " << opt->name << "\n";

   for (int i = 0; i < parse.nonOptionsCount(); ++i)
     std::cout << "Non-option #" << i << ": " << parse.nonOption(i) << "\n";
 }

然而得到错误

错误1错误C2057:预期的常量表达式
错误2错误C2466:无法分配常量大小为0的数组 错误3错误C2133:'选项':未知大小 错误4错误C2133:'缓冲区':未知大小

有关如何解决此问题的任何建议吗?

1 个答案:

答案 0 :(得分:1)

option::Option options[stats.options_max], buffer[stats.buffer_max];

数组在编译时必须具有已知大小。改为使用向量。

vector<option::Option> options(stats.options_max), buffer(stats.buffer_max);