我正在开发一个光线追踪项目。所以,我有很多 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/