【问题标题】:Error messages: with Copy constructor and Overloaded assignment operator错误消息:使用复制构造函数和重载赋值运算符
【发布时间】:2019-11-23 13:36:54
【问题描述】:

这是我之前提出的一个问题。 我使用论坛建议的复制构造函数得到编译错误。

class A
{
private:
    int a;
    int b;


public:

    A() { a = 0; b = 0; }
    int getA() { return a; }
    int getB() { return b; }
    A(const Beta& b) :a{ *b.X() }, b{ *b.Y } {} 

};

class Beta
{
private:
    int *x;
    int *y;

public:
    Beta(int a, int b) { x =&a; y = &b; }
    int* X() { return x; }
    int* Y() { return y; }

};

int main()
{
    B aObject;
    Alpha a1 = aBObject;
    Alpha a2;
    a2 = aBObject;

    return 0;

}

复制构造函数参数中没有 const Alpha(Beta& be)

Error   C2061   syntax error: identifier 'Beta' 
Error   C2228   left of '.getY' must have class/struct/union 
Error   C2228   left of '.getX' must have class/struct/union 
Error   C2679   binary '=': no operator found which takes a right-hand operand of type 'Beta' (or there is no acceptable conversion)
Error   C2440   'initializing': cannot convert from 'Beta' to 'Alpha' 
Error   C2065   'be': undeclared identifier 
Error   C2535   'Alpha::Alpha(void)': member function already defined or declared 

在复制构造函数参数中使用 const Alpha(const Beta& be)

Error (active)  the object has type qualifiers that are not compatible with the member function "Beta::getX" 
Error (active)  the object has type qualifiers that are not compatible with the member function "Beta::getY" 

Error   C2061   syntax error: identifier 'Beta' 
Error   C2228   left of '.getY' must have class/struct/union 
Error   C2228   left of '.getX' must have class/struct/union 
Error   C2679   binary '=': no operator found which takes a right-hand operand of type 'Beta' (or there is no acceptable conversion)
Error   C2440   'initializing': cannot convert from 'Beta' to 'Alpha' 
Error   C2065   'be': undeclared identifier 
Error   C2535   'Alpha::Alpha(void)': member function already defined or declared

【问题讨论】:

  • Alpha(const Beta& be) 不是复制构造函数。 Beta 需要在 Alpha 使用之前声明。

标签: c++ overloading copy-constructor assignment-operator


【解决方案1】:

您的代码中没有复制构造函数(或赋值运算符)。但无论如何,这不是问题。他们发布您的Alpha 代码在定义Beta 之前使用Beta。这就是编译器所抱怨的,它在您第一次使用它时无法识别Beta

只需移动Beta,使其定义在Alpha 之前,一切都会编译。

【讨论】:

    【解决方案2】:

    这是你想要的吗?

    class Beta
    {
    private:
        int *x;
        int *y;
    
    public:
        Beta() { x = nullptr; y = nullptr; }
        int* getX() { return x; }
        int* getY() { return y; }
    
    };
    
    class Alpha
    {
    private:
        int a;
        int b;
    
    
    public:
    
        Alpha() { a = 0; b = 0; }
        int getA() { return a; }
        int getB() { return b; }
        Alpha( Beta& be) :a{ *be.getX() }, b{ *be.getY() } {} 
    //It is not copy Constructor. You can't pass const reference here because getX getY returns pointer. So it could break const contract
    
    };
    
    
    
    int main()
    {
        Beta aBetaObject;
        Alpha a1 = aBetaObject;
        Alpha a2;
        a2 = aBetaObject;
    
        return 0;
    
    }
    

    【讨论】:

    • 如果您将 Beta get 方法声明为 const,则可以传递 const Beta&,例如 int* getX() const { return x; }
    • 更多的是 Paul 提出的,getXgetY 返回一个指针与Beta& be 必须是非常量的原因无关给定Beta 的定义。这仅仅是因为这些成员函数被声明为非常量。返回的指针值与它无关。
    • 是的,我的错。但是 AFAIK 你不应该做类似 int* getX() const { return x; } 因为你保证不会改变 Beta 的内部状态,但你真的可以做到
    • 谢谢,我已经改变了类的顺序,但仍然得到错误 C2276 '*": Alpha(Beta& be) 上绑定成员函数表达式的非法操作 :a{ *be.getX() } , b{ *be.getY() } {}. 这段代码是否足以运行语句 I. Alpha a1 = aBetaObject; II. Alpha a2; III. a2 = aBetaObject;
    猜你喜欢
    • 1970-01-01
    • 2014-10-12
    • 2023-03-29
    • 2013-10-23
    • 2012-03-01
    • 1970-01-01
    • 2011-07-19
    • 1970-01-01
    相关资源
    最近更新 更多