2012-07-25 53 views
4

我經歷了gameinsitute的C++程序設計課程,並有運算符重載的例子,我經常得到一個敵不過運營商+錯誤操作符重載

main.cpp|20|error: no match for ‘operator+’ in ‘v + w’

,我不知道在哪裏了問題是。

的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; 
} 

感謝您的幫助提前!

+5

您提供的代碼按原樣編譯。 – 2012-07-25 13:12:41

+2

'operator ='有一個可疑的實現,但不是無效的方式。 – 2012-07-25 13:15:35

+0

你真的確定你發佈了正確的錯誤和正確的相應代碼嗎?我不知何故懷疑這個錯誤真的來自'Vector3 v();',在這種情況下'v'就是函數聲明。 – 2012-07-25 13:24:37

回答

-1

在旁註中,這些重載有一些問題,您將返回本地引用。我很抱歉,這不是一個答案,我可能會爲此付出代價,但仍然是:

以運算符=爲例。它應該是

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

否則,你正在返回一個本地引用,這將是垃圾,一旦此函數退出。運營商+ - 也是同樣的問題。 至於operator *,你正在做一個點乘積,它與矢量乘法不一樣。爲此,我建議編寫()稱爲點一個函數,它接受的另一種載體,並進行自我和第二矢量之間的點積

另外,您應該使用Eigen library

如果你能向我們提供您怎麼編譯代碼,也許我們可以幫助

+0

運算符不返回引用。 'operator ='具有令人驚訝的行爲,但不是無效的代碼。 – 2012-07-25 13:57:20

+0

是的,除了他返回的局部變量,它應該在此運算符的範圍之外未定義= – 2012-07-25 13:59:27

+0

他是operator =返回局部變量的副本,所以除了令人驚訝的行爲之外沒有問題。 – 2012-07-25 16:38:32

1

您的operator+沒有任何問題 - 這是您的operator=這是錯誤的,並導致operator+的結果不會做您認爲正在做的事情。解決這個問題將解決你的問題。

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