The "this" pointer cannot be converted from "const vector3d" to "vector3d &" error
I compile the vector3d class in vs2005.
Error c2662: "vector3d: dotp": the "This" pointer cannot be converted from "const vector3d" to "vector3d &"
How can we correct it? This class is also written by others, and there is no compilation error.
# 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 = magn.pdf ();
If (M> 1.0e-6 ){
// The problem lies here !!!!
// ====================================
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 = magn.pdf ();
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 beginning to programming read (791) Comments (2) EDIT collection reference category: key code
Comment
# Re: the "this" pointer cannot be converted from "const vector3d" to "vector3d &" error
Shi chuanhong
The reason is not that * This cannot be converted to const vector3d &, but the constant member function (double angle () const) cannot call non-
Common member functions (double dotp (const vector3d & V1, const vector3d &
V2). If you change dotp to the following definition: Double dotp (const vector3d & V1, const
Vector3d & V2) Const.
The problem is solved. Reply to more comments
# Re: the "this" pointer cannot be converted from "const vector3d" to "vector3d &" error
Nico
Double dotp (const vector3d & V1, const vector3d & V2) const
{
Return (v1.x * v2.x + v1.y * v2.y + v1.z * v2.z );
}
Reply to more comments