简体   繁体   English

对构造函数的调用不能出现在常量表达式中

[英]a call to a constructor cannot appear in a constant-expression

While I run make build for the project DeSiNe , I am getting the error: a call to a constructor cannot appear in a constant-expression 当我为DeSiNe项目运行make build时,出现错误: a call to a constructor cannot appear in a constant-expression

$ make build
mkdir -m 755 -p obj/Algorithm
g++ -Wall -DNO_TIMER -DNO_TRACES  -O3 -funroll-loops -finline-functions -fexpensive-optimizations -Isrc -o obj/Algorithm/Algorithm.o -c src/Algorithm/Algorithm.cpp
src/Network/Link.h:44:42: error: a call to a constructor cannot appear in a constant-expression
     static const double METRIC_MIN = 1.0/DBL_MAX; // to prevent metric to be 0
                                          ^
src/Network/Link.h:45:38: error: a call to a constructor cannot appear in a constant-expression
     static const double METRIC_MAX = DBL_MAX;
                                      ^

As per Call to a constructor cannot appear in a constant-expression if I change the code in side the Link class definition in Network\\Link.h from 按照我的Network\\Link.h ,如果我更改Network\\Link.h Link类定义中的代码, 则无法在常量表达式中出现对构造函数的调用

static const double METRIC_MIN = 1.0/DBL_MAX; // to prevent metric to be 0
static const double METRIC_MAX = DBL_MAX;

to

static const double METRIC_MIN; // to prevent metric to be 0
double METRIC_MIN = 1.0/DBL_MAX;
static const double METRIC_MAX;
double METRIC_MAX = DBL_MAX;

I receive 我收到

error: ‘double Link::METRIC_MIN’ conflicts with a previous declaration
double METRIC_MIN = 1.0/DBL_MAX;

Added in Link.cpp [See DeSiNe link above for full code please] 已在Link.cpp中添加[请参见上面的DeSiNe链接以获取完整代码]

double METRIC_MIN = 1.0/DBL_MAX;
double METRIC_MAX = DBL_MAX;

as suggested by @immibis 如@immibis建议

The other SO question's answer that you linked to was unclear about where to write the lines. 您链接到的另一个SO问题的答案不清楚在哪里写这些行。 I have edited it so hopefully nobody else is misled. 我已经对其进行了编辑,因此希望没有其他人被误导。

To fix the compilation errors in C++03, change: 要修复C ++ 03中的编译错误,请更改:

static const double METRIC_MIN = 1.0/DBL_MAX; // to prevent metric to be 0
static const double METRIC_MAX = DBL_MAX;

to: 至:

static const double METRIC_MIN; // to prevent metric to be 0
static const double METRIC_MAX;

and then in exactly one .cpp file (it doesn't matter which, so long as Link.h is included by that file) add the following lines at file scope: 然后在一个.cpp文件中(只要该文件包含Link.h ,就Link.h ),在文件范围内添加以下行:

const double Link::METRIC_MIN = 1.0/DBL_MAX;
const double Link::METRIC_MAX = DBL_MAX;

However there may be further issues. 但是 ,可能还有其他问题。 Clearly whoever wrote this code was using a compiler with an extension that treated DBL_MAX as a constant expression prior to C++11. 显然,编写此代码的人都在使用带有扩展名的编译器,该扩展程序将DBL_MAX视为C ++ 11之前的常量表达式。 It's possible that the rest of the code relies on the value of these constants being visible in the header. 其余代码有可能依赖于这些常量在标头中可见的值。

If you fix this bug then get compilation errors from other parts of the code relating to these variables then you may have to try a different solution (which will involve more code editing). 如果修复了该错误,然后从与这些变量相关的代码的其他部分得到了编译错误,则您可能必须尝试其他解决方案(这将涉及更多的代码编辑)。

#define METRIC_MIN DBL_MIN
#define METRIC_MAX DBL_MAX

seems to work well 似乎运作良好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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