C2678 - 未找到任何运营商

时间:2017-03-20 23:27:37

标签: c++ binary-operators

如果我尝试编译,我会收到以下错误: C2678

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所需的所有函数中,不需要任何 - 运算符,为什么它会抱怨这个实现?

1 个答案:

答案 0 :(得分:3)

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

这声明了一个左侧必须是非const的运算符。埃尔戈:

  

二进制&#39; - &#39;:找不到哪个运算符采用类型为&#39; const D3O :: Point&#39;的类型的左手操作数。

它不能使用该功能,因为左侧是const。我认为你的意思是这样做:

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