Search code examples
c++classtemplatesinstanceraytracing

Expected expression before > token in template class


I'm trying to instantiate a template class inside one of its functions, but for some reason I am unable to do so. It tells gives me the error and points to the beginning of the line, which doesn't make sense.

template<typename O>
class ray{

...

color<O> ray_color(const ray<O>& ray, const hittable_list<O>& world, O t_Min, O t_Max, int depth){

    hit_record<O> rec;
    if (world.hit(ray, t_Min, t_Max, rec)){
          Vec3<O> target = rec.p + rec.normal + Vec3<O>::random_in_unit_sphere();
          ray<O> new_ray(rec.p, (target - rec.p));  //line where it fails
          ray.direction() = target - rec.p;
          return ray_color(new_ray, world, t_Min, t_Max, depth-1) * 0.5;
    }

It gives me this error:

Ray.h: In member function ‘color ray::ray_color(const ray&, const hittable_list&, O, O, int)’:
Ray.h:61:16: error: expected primary-expression before ‘>’ token
ray<O> new_ray(rec.p, (target - rec.p));
     ^

Which also causes new_array to not be declared and gives subsequent errors.

I have created ray objects elsewhere with the same syntax but only this one gives me an error. What gives?

Note: I know class functions should be implemented in a separate cpp file. I'm planning to do all cleanups like that after I get my program working.


Solution

  • One of your parameters is named ray, so in the function body any references to ray refer to that parameter, not the class name the function is defined in.

    The solution is simple: call your parameter something else.