简体   繁体   中英

Convert small bit of code from c++11 to c++03?

I'm a social scientist trying to compile C++ code, and hitting small problem I think is trivially easy to fix if you know C++, but inscrutable to me.

Trying to compile on V2008, but my code seems to use some C++11 syntax tricks.

In principle, I think this is solution , but I really don't understand C++ so not sure how to implement.

Code block:

Optimiser::Optimiser(double eps, double delta, unsigned long max_itr, long random_order)
{
  this->eps = eps;
  this->delta = delta;
  this->max_itr = max_itr;
  this->random_order = random_order;
}

Optimiser::Optimiser() : Optimiser::Optimiser(1e-5, 1e-2, 1000, 1)
{
  //ctor
}

And the error I get (referring to the Optimiser::Optimiser() : Optimiser::Optimiser(1e-5, 1e-2, 1000,1) line) is:

c:\Program Files (x86)\Common Files\Microsoft\Visual C++ for Python\9.0\VC\Include\xlocale(342) : warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
src\Optimiser.cpp(39) : error C2039: '{ctor}' : is not a member of 'Optimiser'        include\Optimiser.h(26) : see declaration of 'Optimiser'
src\Optimiser.cpp(40) : error C2614: 'Optimiser' : illegal member initialization: 'Optimiser' is not a base or member
error: command '"c:\Program Files (x86)\Common Files\Microsoft\Visual C++ for Python\9.0\VC\Bin\amd64\cl.exe"' failed with exit status 2

Delegating constructors were not allowed until C++11. That being said, you'll be duplicating code but you can simply change your second constructor to

Optimiser::Optimiser()
{
    eps = 1.0e-5;
    delta = 1.0e-2;
    max_itr = 1000UL;
    random_order = 1L;
    //rest of your ctor
}

Probably most straightforward solution is to remove current default constructor and provide default arguments in the other one

class Optimiser {
    public:
        Optimiser(double eps = 1e-5, double delta = 1e-2, unsigned long max_itr = 1000, long random_order = 1);
    ...      
 }

Constructor definition may stay as it is, although it's better to use initialization list

Optimiser::Optimiser(double p_eps, double p_delta, unsigned long p_max_itr, long p_random_order):
    eps(p_eps),
    delta(p_delta),
    max_itr(p_max_itr),
    random_order(p_random_order)
{
}

I recommend changing this:

Optimiser::Optimiser() : Optimiser::Optimiser(1e-5, 1e-2, 1000, 1)
{
  //ctor
}

to this:

Optimizer::Optimizer()
: eps(1.0e-5), delta(1.0e-2), max_itr(1000), random_order(1)
{
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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