2

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

$ 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

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;
Community
  • 1
  • 1
sinhayash
  • 2,693
  • 4
  • 19
  • 51
  • I have referenced that question already and described the difficulties. Please read the question fully and then comment – sinhayash Apr 06 '16 at 03:50
  • 2
    No, you did *not* follow the answer to that question. – user253751 Apr 06 '16 at 03:55
  • ahh, sorry. The definition and declaration definitely should match. So you need to use `static const double` in the declaration (presumably in a `.h` file) and `const double` in definition (in a `.cpp` file) – user3159253 Apr 06 '16 at 03:55
  • alternatively, you may want to use constexpr's. Certainly, if your compiler [decently supports](http://stackoverflow.com/questions/23454507/initializing-a-static-constexpr-double) them – user3159253 Apr 06 '16 at 03:59
  • @immibis I did now, can you help further? – sinhayash Apr 06 '16 at 04:04
  • 1. _const double_ in definition. 2. Move the definition out of `.h` file, otherwise you'll get linkage errors – user3159253 Apr 06 '16 at 04:10
  • @sinhayash You still have not followed it. Or the code in the question is not the code you're trying to use. – user253751 Apr 06 '16 at 04:31
  • @immibis can you please help me with an example? – sinhayash Apr 06 '16 at 04:33
  • @sinhayash Well, the answer says to put `const Type Class::Variable = Value;` in one source file (where Type, Class, etc were the actual details of that question and are different for yours), and what you did was put `Type Variable = Value;` in a header file? – user253751 Apr 06 '16 at 05:15

3 Answers3

0

Added in Link.cpp [See DeSiNe link above for full code please]

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

as suggested by @immibis

sinhayash
  • 2,693
  • 4
  • 19
  • 51
0

The other SO question's answer that you linked to was unclear about where to write the lines. I have edited it so hopefully nobody else is misled.

To fix the compilation errors in C++03, change:

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:

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. 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).

M.M
  • 138,810
  • 21
  • 208
  • 365
  • Shall I include float.h as indicated here: http://www.cplusplus.com/reference/cfloat/ to fix errors? – sinhayash Apr 06 '16 at 04:46
  • `DBL_MAX` is defined by float.h, so that should be included already. Your error messages in your question indicate that it was included already. You can add it in anyway though if you want – M.M Apr 06 '16 at 04:46
  • It is included already; can we just do it like `define METRIC_MAX DBL_MAX`? Isn't that easy to work? – sinhayash Apr 06 '16 at 04:48
  • Are you still having trouble with the solution I suggested? If so, what? – M.M Apr 06 '16 at 04:54
  • You shouldn't use the `#define`. The solution of that ilk would be to place `const double METRIC_MAX = DBL_MAX;` etc. outside of the class and take out the in-class definitions, but then you also have to search all the rest of the code to make sure there is no clash with other variables called `METRIC_MAX` (since you now are introducing it into a greater scope). In fact you would use some other tricks to avoid clashes. I didnt want to go into this yet unless it turns out the first solution doesn't work. – M.M Apr 06 '16 at 04:55
0
#define METRIC_MIN DBL_MIN
#define METRIC_MAX DBL_MAX

seems to work well

sinhayash
  • 2,693
  • 4
  • 19
  • 51