[Book 1] Listing 26 Section 6.8 Infinity is undefined
In the listing:
#ifndef INTERVAL_H
#define INTERVAL_H
class interval {
public:
double min, max;
interval() : min(+infinity), max(-infinity) {} // Default interval is empty
interval(double _min, double _max) : min(_min), max(_max) {}
bool contains(double x) const {
return min <= x && x <= max;
}
bool surrounds(double x) const {
return min < x && x < max;
}
static const interval empty, universe;
};
const static interval empty (+infinity, -infinity);
const static interval universe(-infinity, +infinity);
#endif
The file rtweekend.h is not included, and thus infinity is undefined. This was mentioned in #1281 but the issue was closed and the listing is still "wrong".
I've run into this as well.
The issue is the common headers need to be included after the double infinity definition.
...
// Common Headers
#include "interval.h"
#include "ray.h"
#include "vec3.h"
#endif
Not so much an issue, but could be worth pointing out.
Having interval.h include rtweekend.h would induce a circular include loop, since rtweekend.h includes interval.h. I suspect that your version of rtweekend.h lacks the inclusion mentioned in book 1, listing 27 ("Including the new interval class").
@solimm4sks — please let me know if I've missed something.
It seems to me that infinity should be defined in interval.h instead of in rtweekend.h, which is where it is currently defined by the time one reaches Book 1 Section 6.8. This would solve the undefined issue, avoid the circular dependency, and still ensure that the const is accessible wherever rtweekend.h is included.
Infinity is not a concept limited to intervals only. It is also used, for example in the camera code. Readers are instructed to always include rtweekend.h as the first header file, which addresses this issue. The original poster missed that.