Search code examples
c++binary-operators

C2678 - no operator found


If I try to compile I get the following error: C2678

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

code creating error:

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));

Definition of angleto:

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

Definition of 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());
}

Definition of ToVector:

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

Why do I get this error? I have included the operator-overwrites in the used namespace.

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

Do I have to pin the class variables X,Y and Z or what is the reason why I get this error? I am really confused, in all functions required to build angleTo, there is no need of any - operator, so why is it complaining about that implementation?


Solution

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

    This declares an operator where the left side must be non-const. Ergo:

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

    It can't use that function because the left side is const. I assume you meant to do this instead:

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