C2678 - 未找到操作员
C2678 - no operator found
如果我尝试编译,我会收到以下错误:
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);}
我是否必须固定 class 变量 X、Y 和 Z,或者出现此错误的原因是什么?我真的很困惑,在构建 angleTo 所需的所有函数中,不需要任何 - 运算符,那么为什么它会抱怨该实现?
const Vector const Vector::operator- (const Vector param) {
这声明了一个运算符,其中左侧必须是非常量。因此:
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
如果我尝试编译,我会收到以下错误: 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);}
我是否必须固定 class 变量 X、Y 和 Z,或者出现此错误的原因是什么?我真的很困惑,在构建 angleTo 所需的所有函数中,不需要任何 - 运算符,那么为什么它会抱怨该实现?
const Vector const Vector::operator- (const Vector param) {
这声明了一个运算符,其中左侧必须是非常量。因此:
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