简体   繁体   English

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

[英]How do I make sure that a declaration has a storage class or type specifier in C++ when using boost's program_options?

I am just beginning to learn about Boost for C++. 我刚开始学习Boost for C ++。 I was following an example that uses the "program_options" library from boost. 我正在关注一个使用boost中的“program_options”库的示例。 Below is my code. 以下是我的代码。 I am using Visual Studio and have already built the boost libraries that need building, added boost to the additional include directory, and added boost to the additional linker directories. 我正在使用Visual Studio并且已经构建了需要构建的boost库,为其他include目录添加了boost,并为其他链接器目录添加了boost。

The issue is with desc.add_options() . 问题出在desc.add_options() Visual Studio says that this declaration has no storage class or type specifier. Visual Studio说该声明没有存储类或类型说明符。 I am unsure of what that means and how to fix it. 我不确定这意味着什么以及如何解决它。 I have looked for solutions, but I have come up empty handed. 我已经找到了解决方案,但我空手而归。 Any help would be awesome. 任何帮助都是极好的。 Thanks! 谢谢!

#include <boost/program_options.hpp>
#include <iostream>

namespace opt = boost::program_options;

opt::options_description desc("All options");


desc.add_options()
    ("apples", opt::value<int>(), "how many apples do you have")
    ("oranges", opt::value<int>(), "how many oranges do you have")
;

You don't want that . 你不希望这样 What the message implies is NOT that you miss some part of a declaration. 消息暗示的并不是你错过了声明的某些部分。

That line is NOT a declaration, and you should not be trying to make it into one. 该行不是声明,您不应该尝试将其合并为一行。

What this is is a classic misunderstanding. 这是一个经典的误解。 The clash arises because the compiler expects only declarations at the global or namespace scope. 冲突的产生是因为编译器期望只能在全局或命名空间范围的声明。 Since you used a statement, it cannot be interpreted as a declaration and hilarity ensues. 由于您使用了一个声明,因此不能将其解释为声明并且随之而来的是欢闹。

Fix it, eg: 修复它,例如:

#include <boost/program_options.hpp>
#include <iostream>

namespace opt = boost::program_options;

int main(int argc, char** argv) {

    opt::options_description desc("All options");

    desc.add_options()
        ("apples", opt::value<int>(), "how many apples do you have")
        ("oranges", opt::value<int>(), "how many oranges do you have")
    ;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM