【问题标题】:no match for operator+ error with operator overloading运算符重载时运算符+错误不匹配
【发布时间】:2012-07-23 22:08:10
【问题描述】:

我正在学习游戏学院的 c++ 编程课程,并且有一个运算符重载的示例,我不断得到一个

main.cpp|20|错误:'v + w'中的'operator+'不匹配

我不知道问题出在哪里。

main.cpp

// main.cpp

#include "Vector3.h"
#include <iostream>

using namespace std;

int main()
{
    float coords[3] = {1.0f, 2.0f, 3.0f};
    Vector3 u;
    Vector3 v(coords);
    Vector3 w(-5.0f, 2.0f, 0.0f);

    cout << "u = ";    u.print();
    cout << "v = ";    v.print();
    cout << "w = ";    w.print();
    cout << endl;

    u = v + w; // this gives the error
    cout << "v + w = ";
    u.print();
    cout << endl;

    v.normalize();
    cout << "unit v = ";
    v.print();
    cout << "v.length() = "<< v.length() << endl;
    cout << endl;

    float dotP = u * w; // this also gives error
    cout << "u * w = " << dotP << endl;

    float* vArray = v.toFloatArray();

    cout <<
          "[0] = " << vArray[0] << ", "
          "[1] = " << vArray[1] << ", "
          "[2] = " << vArray[2] << endl <<endl;

    cout << "Input vector..." << endl;
    Vector3 m;
    m.input();
    cout << "m = ";
    m.print();

    return 0;
}

Vector3.h

#ifndef VECTOR3_H
#define VECTOR3_H

#include <iostream>

class Vector3
{
public:

    // constructors
    Vector3();
    Vector3(float coords[3]);
    Vector3(float x, float y, float z);
    Vector3(const Vector3& vec);

    // methods
    float  length();
    void    normalize();
    float* toFloatArray();
    void    print();
    void    input();

    // operators
    Vector3 operator=(const Vector3& rhs);
    Vector3 operator+(const Vector3& rhs) const;
    Vector3 operator-(const Vector3& rhs) const;
    float  operator*(const Vector3& rhs) const;
    Vector3 operator*(float scalar) const;

    // fields
    float mX;
    float mY;
    float mZ;
};

#endif // VECTOR3_H

Vector3.cpp

#include "Vector3.h"
#include <cmath>
#include <iostream>

using std::cout;
using std::cin;

Vector3::Vector3()
{
    mX = 0.0f;
    mY = 0.0f;
    mZ = 0.0f;
}

Vector3::Vector3(float coords[3])
{
    mX = coords[0];
    mY = coords[1];
    mZ = coords[2];
}

Vector3::Vector3(float x, float y, float z)
{
    mX = x;
    mY = y;
    mZ = z;
}

Vector3::Vector3(const Vector3& vec)
{
    mX = vec.mX;
    mY = vec.mY;
    mZ = vec.mZ;
}

float Vector3::length()
{
    return sqrt(mX*mX + mY*mY + mZ*mZ);
}

void Vector3::normalize()
{
    float len = length();
    mX /= len;
    mY /= len;
    mZ /= len;
}

float* Vector3::toFloatArray()
{
    return &mX;
}

void Vector3::print()
{
    cout << "<" << mX << ", " << mY << ", " << mZ << "> \n";
}

void Vector3::input()
{
    cout << "Enter x: ";
    cin >> mX;
    cout << "Enter y: ";
    cin >> mY;
    cout << "Enter z: ";
    cin >> mZ;
}

//operators

Vector3 Vector3::operator=(const Vector3& rhs)
{
    Vector3 vTemp;
    vTemp.mX = rhs.mX;
    vTemp.mY = rhs.mY;
    vTemp.mZ = rhs.mZ;

    return vTemp;
}

Vector3 Vector3::operator+(const Vector3& rhs) const
{
    Vector3 sum;
    sum.mX = mX + rhs.mX;
    sum.mY = mY + rhs.mY;
    sum.mZ = mZ + rhs.mZ;

    return sum;
}

Vector3 Vector3::operator-(const Vector3& rhs) const
{
    Vector3 dif;
    dif.mX = mX - rhs.mX;
    dif.mY = mY - rhs.mY;
    dif.mZ = mZ - rhs.mZ;

    return dif;
}

float Vector3::operator*(const Vector3& rhs) const
{
    float dotP = mX*rhs.mX + mY*rhs.mY + mZ*rhs.mZ;

    return dotP;
}

Vector3 Vector3::operator*(float scalar) const
{
    Vector3 p;
    p.mX = mX * scalar;
    p.mY = mY * scalar;
    p.mZ = mZ * scalar;

    return p;
}

提前感谢您的帮助!

【问题讨论】:

  • 您提供的代码按原样编译。
  • operator= 有一个可疑的实现,但不是无效的。
  • 您确定您发布了正确的错误和正确的相应代码吗?我不知何故怀疑错误确实来自Vector3 v();之类的东西,在这种情况下v将是函数声明。
  • g++ 3.4.4 或 4.5.3 没有问题。您可能可能选择与您预期不同的“Vector3.h”。检查g++ -E main.cpp的输出。

标签: c++ overloading operator-keyword


【解决方案1】:

您的 operator+ 没有任何问题 - 是您的 operator= 错误并导致 operator+ 没有按照您认为的那样做。解决这个问题将解决您的问题。

Vector3 Vector3::operator=(const Vector3& rhs)
{
    mX = rhs.mX;
    mY = rhs.mY;
    mZ = rhs.mZ;
    return *this;
}

【讨论】:

    【解决方案2】:

    附带说明,其中一些重载存在问题,您正在返回本地引用。很抱歉,这不是一个答案,我可能会对此投反对票,但仍然:

    以operator=为例。应该是

    Vector3& Vector3::operator=(const Vector3& rhs)
    {
      this->mX = rhs.mX;
      this->mY = rhs.mY;
      this->mZ = rhs.mZ;
      return (*this);
    }
    

    否则,一旦此函数退出,您将返回一个本地引用,该引用将成为垃圾。运算符 +- 也有同样的问题。 至于 operator* 你正在做一个点积,这与向量乘法不同。为此,我建议编写一个名为 dot() 的函数,它采用另一个向量并在自身和第二个向量之间执行点积

    或者你应该使用Eigen library

    如果您能提供我们如何编译您的代码,也许我们可以提供帮助

    【讨论】:

    • 运算符不返回引用。 operator= 有一个令人惊讶的行为,但不是无效代码。
    • 是的,只是他返回的是局部变量,在这个操作符的范围之外应该是未定义的=
    • 他的 operator= 返回一个局部变量的副本,所以除了令人惊讶的行为之外没有任何问题。
    猜你喜欢
    • 2018-07-19
    • 2017-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-18
    相关资源
    最近更新 更多