Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am i getting error for this code? [duplicate]

Tags:

c++

visual-c++

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 &'
like image 612
MetallicPriest Avatar asked Sep 16 '26 06:09

MetallicPriest


1 Answers

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) {}
like image 116
Ed S. Avatar answered Sep 18 '26 20:09

Ed S.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!