不能将“this”指针从“const Vector3D”转换为“Vector3D &" 错误

不能将“this”指针从“const Vector3D”转换为“Vector3D &" 错误

我在VS2005中编译Vector3D类出现
error C2662: “Vector3D::dotP”: 不能将“this”指针从“const Vector3D”转换为“Vector3D &”

怎么样改正呢,这个类也是看到别人这样写的,编译也没有错误。

#pragma once

#define M_PI 3.141
#include <math.h>

class Vector3D{
 Vector3D(){x=y=z=0.0;}
 Vector3D(double vx, double vy,double vz=0.0){
  x = vx;
  y = vy;
  z = vz;
 }
 
 double magnitude() const{
  return sqrt(x*x+y*y+z*z);
 }

 double dotP(const Vector3D& v1,const Vector3D& v2){
  return (v1.x*v2.x+v1.y*v2.y+v1.z*v2.z);
 }

 // get the vector angle
 double angle() const{
  double ret = 0.0;
  double m = magnitude();

  if (m>1.0e-6) {

// 问题出在这里!!!!
// ==============================
   double dp = dotP(*this,Vector3D(1.0,0.0));
//==============================

   if (dp/m>=1.0) {
    ret = 0.0;
   }
   else if (dp/m<-1.0) {
    ret = M_PI;
   }
   else {
    ret = acos( dp / m);
   }
   if (y<0.0) {
    ret = 2*M_PI - ret;
   }
  }
  return ret;
 }


protected:
 double x;
 double y;
 double z;

};#pragma once

#define M_PI 3.141
#include <math.h>

class Vector3D{
 Vector3D(){x=y=z=0.0;}
 Vector3D(double vx, double vy,double vz=0.0){
  x = vx;
  y = vy;
  z = vz;
 }
 
 double magnitude() const{
  return sqrt(x*x+y*y+z*z);
 }

 double dotP(const Vector3D& v1,const Vector3D& v2){
  return (v1.x*v2.x+v1.y*v2.y+v1.z*v2.z);
 }

 // get the vector angle
 double angle() const{
  double ret = 0.0;
  double m = magnitude();

  if (m>1.0e-6) {
   double dp = dotP(*this,Vector3D(1.0,0.0));

   if (dp/m>=1.0) {
    ret = 0.0;
   }
   else if (dp/m<-1.0) {
    ret = M_PI;
   }
   else {
    ret = acos( dp / m);
   }
   if (y<0.0) {
    ret = 2*M_PI - ret;
   }
  }
  return ret;
 }


protected:
 double x;
 double y;
 double z;

};

你可能感兴趣的:(不能将“this”指针从“const Vector3D”转换为“Vector3D &" 错误)