Hello!

I am new to C ++ and I still don’t understand when what methods are called by classes in C ++, so the question arose:

There is a configuration file that needs to be parsed, I decided to choose the boost library. Everything is relatively simple in it:

namespace po = boost::program_options; int a; po::options_description desc("conf_file"); desc.add_options() ("a", po::value<int>(&a)); po::variables_map vm; std::ifstream settings_file("conf.ini"); po::store(po::parse_config_file(settings_file, desc), vm); settings_file.close(); po::notify(vm); 

Everything works well, but actually the question is: how to make it work if a - std :: complex? If just changing the 6th line to po :: value <std :: complex <double >> (& a), then in what form should the variable be set in the file itself?

    1 answer 1

    In such cases, you can do the opposite. To begin with, we will try to save the value in the file and see what it writes there. Most likely the format will be clear. And most likely there will be something in the form of a bracket, a number, a comma, a number, a bracket - (2,3) or (3.14, 2.78) .

    So, first write the code that saves the value, and then look in the resulting file. After that, you can experiment.

    PS Most likely the boost uses the operators << and >> . Therefore, you can do as I

     #include <iostream> #include <complex> using namespace std; int main() { std::complex<double> a(1,2); std::cout << a << endl; return 0; } 

    reading also works, check.

    • Yes, thanks for the advice. I will make only one note: I had to stop using program_options in favor of property_tree. The first, as I read on the Internet, is needed more for parsing the command line and, moreover, does not know how to write to a file. In the case of property_tree, everything turned out to be simple: the value is output to the file in the same way as <<, respectively, there were no problems with its reading. - Alexander Demidov