program_options and ->notify(&your_function)

Hi, I'm new to boost (and c++) and I've just started using program_options. Great concept and code, it has saved me a lot of time/effort, thanks. I am having a problem with the ->notify(&your_function) in add_options. I get a compile error (g++ on redhat): "invalid use of void expression". If I comment out the ->notify(&func) part, the code compiles. This is the code: void Report::PrintName(const string name) { cout << " the log name is " << name << "\n"; } void Report::InitCmdOpts(void) { try { p_opts = new po::options_description("Reporting"); p_opts->add_options() ("log", po::value< string >() ->default_value("sys.log") ->notify( &Report::PrintName)) ; } ...catch code... } Any help/ideas appreciated. I'm hoping it's something simple and my lack of c++ knowledge is showing. I've already simplified the code and tried several modifications, but the compile error persists. thanks, susan. __________________________________ Do you Yahoo!? Yahoo! Mail - You care about security. So do we. http://promotions.yahoo.com/new_mail

Hi Susan,
void Report::PrintName(const string name) { cout << " the log name is " << name << "\n"; }
void Report::InitCmdOpts(void) { try { p_opts = new po::options_description("Reporting"); p_opts->add_options() ("log", po::value< string >() ->default_value("sys.log") ->notify( &Report::PrintName)) ;
In this context the library expects you to pass a function that takes
'string' and returns nothing. However, you pass a member function, which
has implicit 'this' argument. I'd suggest this:
#include

In this context the library expects you to pass a function that takes 'string' and returns nothing. However, you pass a member function, which has implicit 'this' argument. I'd suggest this:
#include
.....
->notify(boost::bind(&Report::PrintName, this, _1))
HTH, Volodya
Hi, Thanks for the quick reply. I tried your suggestion and it didn't get rid of the compile error. I had also previously tried making PrintName be a function (instead of a member function) and it didn't compile either. To simplify everything, I took the first example from the tutorial, first.cpp, and added a notify and a new, simple function (boost 1.32, libs/program_options/example). In first.cpp, change line 23: ("compression", po::value<int>(), "set compression level") ---to--- ("compression", po::value<int>()->default(2)->notify(&PrintComp), "set compression level") then add this function before main: void PrintComp(int compr) { cout << "***Compression level is now " << compr << "\n"; } The compile error is (still) 'invalid use of void expression'. Does my code above work for you? thanks again for your help, susan.

Susan Flynn wrote:
In this context the library expects you to pass a function that takes 'string' and returns nothing. However, you pass a member function, which has implicit 'this' argument. I'd suggest this:
#include
.....
->notify(boost::bind(&Report::PrintName, this, _1))
HTH, Volodya
Hi, Thanks for the quick reply. I tried your suggestion and it didn't get rid of the compile error. I had also previously tried making PrintName be a function (instead of a member function) and it didn't compile either. To simplify everything, I took the first example from the tutorial, first.cpp, and added a notify and a new, simple function (boost 1.32, libs/program_options/example). In first.cpp, change line 23: ("compression", po::value<int>(), "set compression level") ---to--- ("compression", po::value<int>()->default(2)->notify(&PrintComp), "set compression level") then add this function before main: void PrintComp(int compr) { cout << "***Compression level is now " << compr << "\n"; }
The compile error is (still) 'invalid use of void expression'. Does my code above work for you?
No, but the attached does work. It differs only in using ->notifier not ->notify The 1.32 docs mistakenly had "notify" in one place, but I believe it's fixed now. Sorry for confusion. For calling member function, you'll still need 'bind'. - Volodya

The 1.32 docs mistakenly had "notify" in one place, but I believe it's fixed now. Sorry for confusion.
For calling member function, you'll still need 'bind'.
Yes, this was the problem. ->notifier works. The on-line copy of the docs still has ->notify, but I'm not sure when/how that gets updated. Thanks for the help! susan.
participants (2)
-
Susan Flynn
-
Vladimir Prus