Basically it is the following code, which cannot pass compiler (g++)
#include <boost/program_options.hpp>
#include <iostream>
using std::cout;
using std::endl;
namespace po = boost::program_options;
class static_class {
public:
static po::options_description cmd_opt; // here is the definition
};
po::options_description static_class::cmd_opt("dummy");
// the line below cannot pass the compiler !!!
static_class::cmd_opt.add_options()
("help", "show usage info.")
;
main() {
cout << static_class::cmd_opt << endl;
}
The error message:
test.cpp:16:1: error: ‘cmd_opt’ in class ‘static_class’ does not name a type
Any idea?
P.S. I am trying to define a separated options_description for each command I need to handle in a small command line environment. I am using bison and flex to parse the command line environment. All arguments of a command will be sent to this static class for argument parsing.
As the argument definition is static, I do not want to make them some sort of data structures in stack (just in my mind may be this is fast and clean). I think these code will be ok if they are not static but what happen if they are?
You're not allowed to have standalone statements at global or namespace scope.
You could get around the issue by using the return value of the extended
add_options
expression to declare and initialize a dummy variable, as below.If your version of C++ doesn't support
auto
like that, then you can use the type's full name,po::options_description_easy_init
, instead.