c++ - 错误 C2666 类似重载

标签 c++ operator-overloading raytracing

我正在开发一个光线追踪项目。所以,我有很多 vector 操作。对于产品操作,我使用了运算符重载,遇到了一个问题。您可以在下面查看详细信息:

这些函数在名为 RayMath.h 的头文件中

//GENERAL INLINES
inline Vector operator*( float c, const Vector& v){ return v * c;   } //Func1
inline Vector operator*( const Vector& v1, Vector& v2 ) { return Vector( v1.x * v2.x, v1.y * v2.y, v1.z * v2.z ); } //Func 2

如果我添加 Func 2,它会为使用 Func1 的操作提供 C2666, more than one operator。如果我不添加 Func 2,我会得到一个 no operator matches 错误。这是使用示例:

这些行位于名为 Renderer.cpp.cpp 文件中

Vector R = ray.direction - 2.f * Dot( ray.direction, N ) * N; //Func1 related
color += trace(  Ray( ( intersectionP + R * EPSILON ), R ) ) * sphere->surfaceColor * sphere->reflection; // Func 2 related

非常感谢您的帮助!

最佳答案

您已经发现 Vector(float) 构造函数导致了问题,因为它提供了从 float 到 vector 的隐式转换。作为基本规则,您应该始终显式声明单参数构造函数:

explicit Vector(float f): x(f), y(f), z(f)

关于c++ - 错误 C2666 类似重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21999130/

相关文章:

graphics - 不同视角的光线追踪

c++ - JUCE - 成员函数不可行 : 'this' Argument Has Type const

c++ - 如何链接多个运算符[]

python - 为什么我的简单 __eq__ 实现给出了 AssertionError?

c++ - 禁止 GLSL 中的递归?

c++ - 光线追踪 - 光线与平行四边形

c++ - 为什么 cin 命令在缓冲区中留下 '\n'?

c++ - 为什么 std::priority_queue 对其容器的元素进行排序?

c++ - 在不知道其元数的情况下绑定(bind)函数的第一个参数

android - 错误 : use of overloaded operator '[]' is ambiguous