0

I am following along with the Ray Tracing in One Weekend book and I'm getting a weird error even though I've made sure my code is identical to the code listed in the book.

In the vec3 class

class vec3{
    public:
        // Default constructor (zero vector)
        vec3() : e{0,0,0} {}
        // Constructor
        vec3(double x, double y, double z) : e{x, y, z} {}
        
        // various functions

    public:
        double e[3];
};

I'm getting this error when compiling:

./vec3.h:12:27: error: expected member name or ';' after declaration specifiers
        vec3() : e{0,0,0} {}
                          ^
./vec3.h:12:19: error: expected '('
        vec3() : e{0,0,0} {}

Is this something to do with the compiler I'm using (g++) or can any of you spot some type of stupid error I'm making? I'm quite new to C++, this would be my second project with it.

Ammar Ahmed
  • 344
  • 3
  • 11
  • 2
    Your code compiles for me. I would guess it's a version of C++ issue. What compiler are you using, and what version of C++ are you asking it to compile? – john Jun 28 '22 at 06:43
  • 3
    Most likely your compiler is too old or set to be using an old version of C++. You should use a recent GCC or Clang and pass something like `-std=c++17` or `-std=c++20` as an argument. – Sebastian Redl Jun 28 '22 at 06:44
  • You can `vec3() = default;` and define `double e[3]{};`. – 273K Jun 28 '22 at 06:45
  • 2
    `e{0,0,0}` requires c++11, does your compiler support c++11 and is it enabled? – Alan Birtles Jun 28 '22 at 06:49
  • It was a version issue. Using the flag @SebastianRedl mentioned worked. – Ammar Ahmed Jun 28 '22 at 08:23

1 Answers1

0

Use

#include <array>

class Vec3 : public std::array<double, 3> {
    // various other functions
};


Vec3 v1;
Vec3 v2{};
Vec3 v3{0,0,0};
Goswin von Brederlow
  • 11,875
  • 2
  • 24
  • 42