Can someone tell me why would the compiler give an error for this.
class A
{
private:
int data;
public:
A();
A(A& a) { this->data = a.data; }
};
void main()
{
A a();
A b(a);
}
The error I get is this.
error C2664: 'A::A(A &)' : cannot convert parameter 1 from
'A (__cdecl *)(void)' to 'A &'
A a();
That is a forward declaration of a function which returns an A and takes no arguments. So, you're trying to pass a function pointer to your constructor, which of course doesn't work as no such constructor exists. If you want to use the default constructor use:
A a;
Of course, that constructor is not defined. You'll need to add a definition (you only wrote a declaration).
As an aside; main is defined to return an int, and you should take a const reference in your constructor.
A(const A &other) : data(other.data) {}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With