# Beginning to 编程

VC++ 方面编程文章

## 不能将“this”指针从“const Vector3D”转换为“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;

};

posted on 2006-05-10 14:42 Beginning to 编程 阅读(5740) 评论(2)  编辑 收藏 引用 所属分类: 关键代码

### 评论

#### #re: 不能将“this”指针从“const Vector3D”转换为“Vector3D &" 错误 2006-05-11 11:59 nico

double dotP(const Vector3D& v1,const Vector3D& v2) const
{
return (v1.x*v2.x+v1.y*v2.y+v1.z*v2.z);
}
回复  更多评论

• 随笔 - 28
• 文章 - 1
• 评论 - 27
• 引用 - 0

•