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; 
9
  • I have referenced that question already and described the difficulties. Please read the question fully and then comment Commented Apr 6, 2016 at 3:50
  • 2
    No, you did not follow the answer to that question. Commented Apr 6, 2016 at 3: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) Commented Apr 6, 2016 at 3:55
  • alternatively, you may want to use constexpr's. Certainly, if your compiler decently supports them Commented Apr 6, 2016 at 3:59
  • @immibis I did now, can you help further? Commented Apr 6, 2016 at 4:04

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

6 Comments

Those double lines must appear in a .cpp file, not a .h file, and they must be const
But that .h file is included in many files, add in all of them?
any one file, or some one specific?
actually, this answer varies depending on whether they are class members or not. It is not clear from your question
Can you see at github.com/TUDelftNAS/DeSiNe, as I have indicated? or tell me how to see at least?
|
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).

5 Comments

Shall I include float.h as indicated here: cplusplus.com/reference/cfloat to fix errors?
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
It is included already; can we just do it like define METRIC_MAX DBL_MAX? Isn't that easy to work?
Are you still having trouble with the solution I suggested? If so, what?
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.
0
#define METRIC_MIN DBL_MIN #define METRIC_MAX DBL_MAX 

seems to work well

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.