超载*操作员只能与一个施工人员一起工作

overloaded * operator work only with one construktor

本文关键字:一个 施工人 工作 一起 操作员 超载      更新时间:2023-10-16

我有类:

class Cline
{
public:
    Cline ():ax(1),by(1),c(0){}
    Cline (double aa, double bb,double cc):ax(aa*(-1)),by(bb),c(cc){
        if(by==0){ 
            exit(1);    
        }   
    }

    Pkt operator* (const Cline & p) 
        {
            if(p.ax != this->ax)    
                {
                      Pkt pkt;
                      double x=this->ax+(p.ax*(-1));
                      double c=(this->c*-1)+p.c;
                      pkt.x=c/x;
                      pkt.y=(this->ax*pkt.x+this->c)/this->by;  
                      return pkt;   
                }
            else 
                {
                    cout<<"no connection";
                }
        }
     void setAX(double w){ax=w;}    
     void setBY(double w){by=w;}
     void setC(double w){c=w;}
     double getAX(){return ax;} 
     double getBY(){return by;}
     double getC(){return c;}
private:
    double ax;
    double by;
    double c;
};

当我使用两次第二个构造函数时:

int main()
{   
Cline z(1,1,1);
Cline w(4,3,-12);
z*w;
return 0;
}

一切都很好,但当我使用第一个和第二个构造函数时:

 int main()
 {  
Cline z();
Cline w(4,3,-12);
z*w;
return 0;
}

我收到错误:

 "no match for operators 'z*w' "

有人能告诉我我做错了什么吗?我不知道我的错误是什么时候犯的:(

问题是,在C++中,这是一个名为z的函数的函数声明,该函数没有参数,并按值返回Cline

Cline z();

你需要

Cline z;   // C++03 and C++11

Cline z{}; // C++11
相关文章: