构造 3D 矢量类时出错

An error occurred while constructing a 3D vector class

提问人:ojipadeson 提问时间:6/21/2023 最后编辑:Zig Razorojipadeson 更新时间:6/21/2023 访问量:56

问:

我正在调试 3D 矢量代码,但出现以下错误

/usr/bin/g++ -fdiagnostics-color=always -g /home/fangrui/vectorFEM/vectorFEM/main.cpp -o /home/fangrui/vectorFEM/vectorFEM/main
In file included from /home/fangrui/vectorFEM/vectorFEM/constant.h:6:0,
                 from /home/fangrui/vectorFEM/vectorFEM/mesh.h:2,
                 from /home/fangrui/vectorFEM/vectorFEM/edgenedelec.h:2,
                 from /home/fangrui/vectorFEM/vectorFEM/main.cpp:1:
/home/fangrui/vectorFEM/vectorFEM/cvector3D.h: In function ‘cvector operator*(double, cvector)’:
/home/fangrui/vectorFEM/vectorFEM/cvector3D.h:39:10: error: cannot bind non-const lvalue reference of type ‘cvector&’ to an rvalue of type ‘cvector’
   return cvector(x1*x2.x, x1*x2.y, x1*x2.z); 
          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...

这是文件中的问题,但我不明白这个错误以及应该进行什么样的更改。除了上面的错误之外,还有一个关于以下内容的警告:cvector3D.hfriend

class "cvector" has no suitable copy constructorC/C++(334)

这是cvector3D.h

# ifndef CVECTOR3D_H
# define CVECTOR3D_H

#include<cmath>
#include<ostream>
#include<iomanip>
using namespace std;

/*A three-dimensional real vector class is defined to facilitate numerical calculations*/
class cvector
{
public:
    double x, y, z;
    //Construct function
    cvector(double _x, double _y, double _z){ x = _x; y = _y; z = _z; } 
    //Copy the constructor
    cvector(cvector &pt){ x = pt.x; y = pt.y; z = pt.z; }
    cvector(){ x = 0; y = 0; z = 0; }
    ~cvector(){};

    //Operator overloading
    cvector operator +();
    cvector operator -();                   //The vector is negated

    friend  ostream  &operator<<(ostream &os, const cvector &x1);

    cvector operator +(cvector x1);
    cvector operator -(cvector x1);
    cvector operator *(double x1);
    cvector operator /(double x1);
    cvector &operator =(cvector x1);
    cvector &operator +=(cvector x1);
    cvector &operator -=(cvector x1);
    cvector &operator *=(double x1);
    cvector &operator /=(double x1);
    int operator ==(cvector x1);
    int operator !=(cvector x1);
    friend cvector operator *(double x1, cvector x2) {
        return cvector(x1*x2.x, x1*x2.y, x1*x2.z); 
    }
    //Member functions
    double  dist(cvector x1);                       
    cvector unit();                               
    double  norm();                           
    friend cvector cross(cvector x1, cvector x2)       
    {
        return cvector(x1.y*x2.z - x1.z*x2.y, x1.z*x2.x - x1.x*x2.z, x1.x*x2.y - x1.y*x2.x);
    }
    friend double dot(cvector x1, cvector x2)      
    {
        return (x1.x*x2.x + x1.y*x2.y + x1.z*x2.z);
    }
} ;

# endif
C++ 复制构造函数 rvalue lvalue

评论

3赞 463035818_is_not_an_ai 6/21/2023
复制构造函数应为cvector(const cvector&)
0赞 n. m. could be an AI 6/21/2023
添加到所有非可变成员函数,例如 和 。constcvector operator +(cvector x1) constdouble dist(cvector x1) const
0赞 john 6/21/2023
在 C++ 中,您不能忽略 .const
0赞 PaulMcKenzie 6/21/2023
中不需要用户定义的复制构造函数。通过提供一个成员变量,你所做的只是有一种方法来引入错误——如果你引入了另一个成员变量,而你忘记在用户定义的构造函数中复制它怎么办?所有成员都是 ,因此该类本身是可安全复制的,即编译器生成的默认复制构造函数完全可以正常工作,并且不会引入错误。底线 -- 删除复制构造函数,直到您真正需要添加一个。cvectordouble
0赞 PaulMcKenzie 6/21/2023
friend cvector cross(cvector x1, cvector x2)如果你习惯了Python,Java,C#或类似的语言,当涉及到C++时,它不会像你认为的那样。这些参数不会创建对传入的引用,而是创建临时副本。如果要在 C++ 中引用,则必须使用 reference 运算符显式使其为人所知。这是程序员在使用其他语言时犯的一个大错误,那就是认为参数传递的工作方式与其他语言相同。cvector&

答:

2赞 Zig Razor 6/21/2023 #1

cvector copy 构造函数格式不正确。

正确的形式是 。cvector(const cvector& cvec)

详细描述:

当好友功能时

friend cvector operator *(double x1, cvector x2) {
        return cvector(x1*x2.x, x1*x2.y, x1*x2.z); 
    }

称为 construnctor 的 被调用。 在构造的对象被 copy 返回后,立即调用了 copy 构造函数,但刚刚创建的 type 的左值 ,即无法转换为 类型的右值。cvectorcvector&non-constcvector

这是因为非常量引用参数(例如 an)只能引用“左值”,这是一个命名变量。int&,

在这种情况下,创建的对象不是命名变量。

评论

1赞 PaulMcKenzie 6/21/2023
房间里的大象是不需要用户定义的复制构造函数。
0赞 Zig Razor 6/21/2023
是的,默认构造就足够了