重载 * 运算符不会给出匹配错误
Overloading * operator gives no match error
嗨,我正在尝试创建一个光线追踪器来渲染多边形的、基于三角形的模型。
我在 point3d.h 中有一个包含 x、y 和 z 坐标的点 3D 结构。
#ifndef __POINT3D_H__
#define __POINT3D_H__
#include <iostream>
using namespace std;
struct Point3D
{
double x;
double y;
double z;
Point3D() : x(0.0), y(0.0), z(0.0) {}
Point3D(const double & nx, const double & ny, const double & nz) : x(nx), y(ny), z(nz) {}
Point3D operator+(const Point3D & rhs) const {
return Point3D(x + rhs.x, y + rhs.y, z + rhs.z); }
Point3D operator-(const Point3D & rhs) const {
return Point3D(x - rhs.x, y - rhs.y, z - rhs.z); }
Point3D operator*(double val) const {
return Point3D(x * val, y * val, z * val); }
Point3D operator/(double val) const {
return Point3D(x / val, y / val, z / val); }
Point3D operator+=(const Point3D & rhs) {
x += rhs.x; y += rhs.y; z += rhs.z; return *this; }
Point3D operator-=(const Point3D & rhs) {
x -= rhs.x; y -= rhs.y; z -= rhs.z; return *this; }
Point3D operator*=(double val) {
x *= val; y *= val; z *= val; return *this; }
Point3D operator/=(double val) {
x /= val; y /= val; z /= val; return *this; }
void print() {
cout << '(' << x << ',' << y << ',' << z << ')';
}
};
#endif
这是我尝试使用 * 运算符将多个两个 Point3D 放在一起的地方
Point3D phong(Point3D mColor, Point3D lColor, Point3D L, Point3D N, Point3D R, Point3D V)
{
Point3D k(1.0, 1.0, 1.0);
Point3D ambient = mColor * k.x;
Point3D diffuse_angle = ((N * L) / (length(N) * length(L)));
Point3D diffuse = lColor * k.y * diffuse_angle;
Point3D specular_angle = ((R * V) / (length(R) * length(V)));
double specular_x = pow(specular_angle.x, 100.0);
double specular_y = pow(specular_angle.y, 100.0);
double specular_z = pow(specular_angle.z, 100.0);
Point3D specular_power(specular_x, specular_y, specular_z);
Point3D specular = lColor * k.z * specular_power;
return ambient + (lColor * (diffuse + specular));
}
当我尝试将多个两个 Point3D 放在一起时,我收到不匹配错误。这是代码失败的地方。我觉得这是一个简单的错误,但我无法弄清楚。我包含Point3d头文件如下:#include"point3d.h"。
Point3D operator*(double val) const
您只有此版本,Point3D * double
,仅此而已,但是您正在尝试使用此运算符进行Point3D * Point3D
。 Point3D
不能从double
隐式构造,所以这就是你出现编译错误的原因。
Point3D operator*(double val) const {
这是用于乘法Point3D * double
。并通过
N * L
你正在尝试做Point3D * Point3D
.
您可以通过为类提供适当的operator*
或通过单参数构造函数提供从双精度到类的转换来纠正此问题。虽然我更喜欢前者。
你应该需要一个这样的函数
Point3D operator *(Point3D &temp) const {
}
由于您没有将两个 3d 点相乘的函数,因此会出现错误。尝试添加此函数。
你需要
一个函数来操作Point3D * Point3D
,它不能适应Point3D::operator*(double val)
的调用。如:
Point3D operator*(const Point3D & rhs) const {
return Point3D(x * rhs.x, y * rhs.y, z * rhs.z); }
相关文章:
- 在运算符重载定义中使用成员函数(const错误)
- 逻辑运算符上出现错误代码 a')'
- 错误 C2679:二进制"<<":未找到采用类型 'std::string_view' 的右侧操作数的运算符(或者没有可接受的转换)
- C++:需要帮助了解运算符重载错误
- 我在 .h 中有一个枚举类,并且在.cpp错误中有一个运算符重载:与"运算符<<不匹配
- C++分数混合比较运算符错误
- 为什么我收到错误:"运算符<<不匹配?
- 迭代器类的重载前缀增量运算符会引发分段错误
- 在 boost::qi 中使用过多的替代运算符会导致分段错误
- 调用运算符时出现链接器错误<<,如何解决?
- 错误:表达式必须具有算术、无作用域枚举或带有运算符重载的指针类型
- 解决错误:运算符 [] 不匹配
- 提升错误:"运算符=="的重载不明确
- 字符串输入中没有匹配的错误运算符错误
- 错误:运算符 = IN C++ 不匹配
- QT 创建器错误(运算符 + 不匹配)
- 错误C++运算符重载
- 错误:运算符==不匹配
- C++ 语法和编译器错误 - 运算符不匹配<<
- C++错误--运算符=不匹配