Search code examples
c++boostboost-program-options

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++. I was following an example that uses the "program_options" library from boost. 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.

The issue is with desc.add_options(). Visual Studio says that this declaration has no storage class or type specifier. 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")
;

Solution

  • 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, e.g.:

    #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")
        ;
    
    }