2017-03-20 55 views
-1

如果我嘗試編譯我收到以下錯誤: C2678C2678 - 沒有操作員發現

binary '-': no operator found which takes a left-hand operand of type 'const D3O::Point' (or there is no acceptable conversion) 

代碼生成錯誤:

forward_list<double> anglelist; 
anglelist.resize(pointlist.max_size()); 
angleto angleTo; 
double test = angleTo(pointlist.front(), angleReference); 
transform(pointlist.begin(), pointlist.end(), anglelist.begin(),bind2nd(angleTo,angleReference)); 

angleto的定義:

struct angleto : public std::binary_function<Point, Vector, const double> 
{ 
     const double operator() (Point a, Vector b) const 
      { return b.angleTo(a.ToVector());} 
     }; 

angleTo的定義:

const double Vector::angleTo(Vector vec) 
{ 
    Vector zVec = this->vecProd(vec); 
    Vector hVec = this->turnAroundAxisfordeg(zVec, 90); 
    if (hVec.smallAngle(vec) <= 90) 
    { 
     return this->smallAngle(vec); 
    } 
    else 
    { 
     return (double)(360.0-this->smallAngle(vec)); 
    } 
} 
const double Vector::smallAngle(Vector vec) 
{ 
    if ((this->value() * vec.value()) == 0) 
    { 
     return (double)0; 
    } 
    else 
    { 
     return 180/M_PI * acos(this->scalar(vec)/(this->value() * vec.value())); 
    } 
} 
const double Vector::value() 
{ 
    return sqrt(this->X * this->X + this->Y * this->Y + this->Z * this->Z); 
} 
const Vector Vector::vecProd(Vector vec) 
{ 
    return Vector(this->Y * vec.Z - this->Z * vec.Y, this->Z * vec.X - this->X * vec.Z, this->X * vec.Y - this->Y * vec.X); 
} 
const Vector Vector::turnAroundAxisfordeg(Vector Axis, double degrees) 
{ 
    if (this->ColinearTo(Axis)) 
    { 
     return Vector(this->X,this->Y,this->Z); 
    } 
    else 
    { 
     double R[3][3] = {}; 
     Vector axis = Axis.getUnitVector(); 
     double deg = degrees/180 * M_PI; 
     R[0][0] = axis.X * axis.X * (1 - cos(deg)) + cos(deg); R[0][1] = axis.X * axis.Y * (1 - cos(deg)) - axis.Z * sin(deg); R[0][2] = axis.X * axis.Z * (1 - cos(deg)) + axis.Y * sin(deg); 
     R[1][0] = axis.Y * axis.X * (1 - cos(deg)) + axis.Z * sin(deg); R[1][1] = axis.Y * axis.Y * (1 - cos(deg)) + cos(deg); R[1][2] = axis.Y * axis.Z * (1 - cos(deg)) - axis.X * sin(deg); 
     R[2][0] = axis.Z * axis.X * (1 - cos(deg)) - axis.Y * sin(deg); R[2][1] = axis.Z * axis.Y * (1 - cos(deg)) + axis.X * sin(deg); R[2][2] = axis.Z * axis.Z * (1 - cos(deg)) + cos(deg); 
     double x = this->X * R[0][0] + this->Y * R[0][1] + this->Z * R[0][2]; 
     double y = this->X * R[1][0] + this->Y * R[1][1] + this->Z * R[1][2]; 
     double z = this->X * R[2][0] + this->Y * R[2][1] + this->Z * R[2][2]; 
     x = this->dRound(x, 15); 
     y = this->dRound(y, 15); 
     z = this->dRound(z, 15); 
     return Vector(x, y, z); 
    } 
} 
const bool Vector::ColinearTo(Vector vec) 
{ 
    return ((this->vecProd(vec)).Value <= 1E-10); 
} 
const double Vector::scalar(Vector vec) 
{ 
    return this->X * vec.X + this->Y * vec.Y + this->Z * vec.Z; 
} 
const Vector Vector::getUnitVector() { 
    return Vector(this->X/this->value(), this->Y/this->value(), this->Z/this->value()); 
} 

ToVector的定義:

const Vector const Point::ToVector() { return Vector(this->X, this->Y, this->Z); } 

爲什麼我得到這個錯誤?我已將運算符覆蓋包含在使用的名稱空間中。

const Vector const Vector::operator- (const Vector param) { double newX, newY, newZ; newX = X - param.X; newY = Y - param.Y; newZ = Z - param.Z; return Vector(newX, newY, newZ);} 

我是否必須固定類變量X,Y和Z,或者我得到這個錯誤的原因是什麼?我非常困惑,在構建angleTo所需的所有函數中,不需要任何 - 運算符,那麼爲什麼它會抱怨實現呢?

+0

在Visual Studio中,「錯誤」窗口只顯示摘要,並且您發佈了錯誤摘要。打開「輸出」窗口查看完整的錯誤信息,包括髮生錯誤的行。現在,你已經發布了'Vector :: operator-(const Vector param)',但這與問題完全無關。該代碼行正在尋找'D3O :: Point :: operator - (???)const'。而且你還沒有發佈任何導致編譯器查找代碼的代碼。請查看[Help Center> Asking](https://stackoverflow.com/help/mcve)瞭解如何提出一個好問題。 –

+0

那麼實際上實施 - 運營商正確解決了這個問題,它編譯:) –

回答

3
const Vector const Vector::operator- (const Vector param) { 

這聲明瞭一個操作符,其左側必須是非常量。 Ergo:

binary '-': no operator found which takes a left-hand operand of type 'const D3O::Point'.

它不能使用該功能,因爲左側是常量。我認爲你應該這樣做:

const Vector  Vector::operator- (const Vector param) const { 
      ^pointless. remove       ^IMPORTANT 
+0

我會檢查,但我認爲這是我的解決方案:),但我仍然不明白爲什麼它調用該運營商。 –

+0

@ Y.S:它不是調用該操作符,那是點 –

+0

就我所知,它試圖調用它找不到的操作符,但是在給定的情況下不使用此操作。不是因爲沒有,而是簡單,因爲沒有呼叫它。在函數的成員中只有 - 操作符是雙重的。 –

相關問題