将一小段代码从c ++ 11转换为c ++ 03?

时间:2015-01-24 20:42:37

标签: c++ c++11 c++03

我是一个尝试编译C ++代码的社会科学家,如果你了解C ++,我认为解决一些小问题很容易解决,但对我来说却是难以理解的。

尝试在V2008上编译,但我的代码似乎使用了一些C ++ 11语法技巧。

In principle, I think this is solution,但我真的不了解C ++,所以不确定如何实施。

代码块:

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
}

我得到的错误(指Optimiser :: Optimiser():Optimiser :: Optimiser(1e-5,1e-2,1000,1)行)是:

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

3 个答案:

答案 0 :(得分:3)

直到C ++ 11才允许委托构造函数。话虽这么说,你将复制代码,但你可以简单地将第二个构造函数更改为

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

答案 1 :(得分:3)

可能最直接的解决方案是删除当前的默认构造函数并在另一个中提供默认参数

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

构造函数定义可以保持不变,尽管使用初始化列表

会更好
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)
{
}

答案 2 :(得分:1)

我建议更改此内容:

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

到此:

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