C++ 命令行参数问题

C++ 命令行参数问题,c++,command-line-arguments,C++,Command Line Arguments,我试图在代码中实现命令行选项。出于某种原因,my-a选项工作正常,但是my-c选项工作不好,即使它们基本相同。当我尝试使用-c选项运行代码时,会收到以下消息 terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_S_construct NULL not valid Aborted 下面是我的代码 int c; std::string config = def+std::

我试图在代码中实现命令行选项。出于某种原因,my-a选项工作正常,但是my-c选项工作不好,即使它们基本相同。当我尝试使用-c选项运行代码时,会收到以下消息

terminate called after throwing an instance of 'std::logic_error'
what():  basic_string::_S_construct NULL not valid
Aborted
下面是我的代码

int c;
std::string config = def+std::string("SamplesConfig.xml");
std::string cal = def+std::string("calibration.bin");
while ((c = getopt(argc, argv, "a:c"))>=0)
{
    switch(c)
    {
        case 'a':
        {
            config = std::string(optarg);
            printf("%s", (char *)config.c_str());
            break;
        }
        case 'c':
        {
            cal = std::string(optarg);
            printf("%s", (char *)cal.c_str());
            break;
        }
        default:
        {
            break;
        }
    }
}

你不应该打电话给getopt(“a:b:c:”)吗?我想,正如您只指定了c一样,optarg到达该点时将为null。

您能减少这个值吗?您知道哪行代码实际上抛出了错误吗?你调试好了吗?谢谢。这是我第一次使用命令行参数,所以我没有意识到我需要以冒号结尾。好吧,你不需要,但省略它意味着它将“c”视为不带参数的选项。a:c:means-a接受一个参数,-c接受一个参数,而a:c意味着-a接受一个参数,但-c不接受