0

This is a question earlier asked by me. I'm getting compilation errors using the copy constructor suggested by the forum.

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;

}

Without const Alpha(Beta& be) in the copy constructor argument

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 

With const Alpha(const Beta& be) in the copy constructor argument

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
1
  • Alpha(const Beta& be) is not a copy constructor. Beta needs to be declared before Alpha can use it. Commented Jul 14, 2019 at 17:51

2 Answers 2

1

Is this what you want?

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;

}
Sign up to request clarification or add additional context in comments.

4 Comments

You can pass const Beta& if you declare the Beta get methods as const, eg int* getX() const { return x; }.
More to the point Paul brought up, getX and getY returning a pointer has nothing to do with the reason Beta& be must be non-const with the given definition of Beta. It's simply because those member functions are declared non-const. The pointer value returns have nothing to do with it.
Yeah, my bad. But AFAIK you should never do something like int* getX() const { return x; } because you promise not to change internal state of Beta but you really can do it
Thanks, I have changed the order of the class but still getting Error C2276 '*": illegal operation on bound member function expression on Alpha( Beta& be) :a{ *be.getX() }, b{ *be.getY() } {}. Is this code sufficent to run the statements I. Alpha a1 = aBetaObject; II. Alpha a2; III. a2 = aBetaObject;
0

There are no copy constructors (or assignment operators) in your code. But anyway that's not the issue. They issue that your code for Alpha uses Beta before Beta has been defined. That's what the compiler is complaining about, it doesn't recognise Beta at the point you first use it.

Just move Beta so it's definition is before Alpha and everything will compile.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.