【问题标题】:Expected expression before > token in template class模板类中 > 标记之前的预期表达式
【发布时间】:2020-12-16 05:58:25
【问题描述】:

我试图在它的一个函数中实例化一个模板类,但由于某种原因我无法这样做。它告诉我错误并指向行首,这没有意义。

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;
    }

它给了我这个错误:

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));
     ^

这也会导致 new_array 未被声明并给出后续错误。

我在其他地方使用相同的语法创建了射线对象,但只有这个对象给我一个错误。什么给了?

注意:我知道类函数应该在单独的 cpp 文件中实现。我计划在我的程序运行后进行所有类似的清理工作。

【问题讨论】:

    标签: c++ class templates instance raytracing


    【解决方案1】:

    您的一个参数被命名为ray,因此在函数体中对ray 的任何引用都指的是该参数,而不是定义函数的类名。

    解决方案很简单:给你的参数起个别的名字。

    【讨论】:

    • 谢谢!这解决了这个问题。不过,我认为是否提供模板和构造函数值并不重要。编译器是否只是在看到 ray 时停止而不进行区分?
    猜你喜欢
    • 2012-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多