The "this" pointer cannot be converted from "const vector3d" to "vector3d &" error

Source: Internet
Author: User
Tags acos
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.