【问题标题】:Overloading * operator gives no match error重载 * 运算符不会给出匹配错误
【发布时间】:2014-12-15 09:42:45
【问题描述】:

您好,我正在尝试创建一个光线追踪器,用于渲染基于三角形的多边形模型。

我在 point3d.h 中有一个点 3D 结构,它包含 x、y 和 z 坐标。

#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"。

【问题讨论】:

  • 你没有定义一个运算符来将两个点相乘,它也没有定义自己。您为什么希望它起作用?
  • 第一件事:no using namespace in header!此外,您的包含保护名称无效。不能以下划线开头。
  • 我已经删除了截图。如果您有文字要发布,请不要发布图片。

标签: c++ operator-overloading operators


【解决方案1】:
Point3D operator*(double val) const

您只有这个版本,Point3D * double,没有别的,但您正尝试将此运算符用于Point3D * Point3DPoint3D 不能从 double 隐式构造,所以这就是编译错误的原因。

【讨论】:

    【解决方案2】:
    Point3D operator*(double val) const { 
    

    这是用于乘法Point3D * double。并且通过

    N * L
    

    你正在尝试做Point3D * Point3D

    您可以通过为您的类提供正确的operator* 或通过单参数构造函数提供从双精度类到您的类的转换来纠正此问题。虽然我更喜欢前者。

    【讨论】:

      【解决方案3】:

      你应该需要这样的函数

      Point3D operator *(Point3D &temp) const {
      
      }
      

      由于您没有将两个 3d 点相乘的功能,您会遇到错误。请尝试添加此功能。

      【讨论】:

        【解决方案4】:

        你需要一个函数来操作Point3D * Point3D,它不能适应Point3D::operator*(double val)的调用。如:

        Point3D operator*(const Point3D & rhs) const {
            return Point3D(x * rhs.x, y * rhs.y, z * rhs.z); }
        

        【讨论】:

          猜你喜欢
          • 2018-07-19
          • 2017-08-10
          • 2012-07-23
          • 1970-01-01
          • 2015-01-02
          • 1970-01-01
          • 1970-01-01
          • 2015-01-14
          • 1970-01-01
          相关资源
          最近更新 更多