I am new to C++, I was reading about constructors and I made a program.
Below is the code:
#include<iostream>
using namespace std;
class Demo{
public:
int a;
Demo(int x,int y){
cout<<"I am default constructor";
}
Demo(Demo &c){
cout<<"I am copy Constructor";
}
};
int main(){
Demo c2=Demo(5,7);
Demo c3=c2;
cout<<"The value is: "<<c2.a;
}
This code was working fine until I added the copy constructor in class and the second line in the main function. As soon as I added these lines, It shows error in the first line of main function.
The error says:
error: no matching function for call to 'Demo::Demo(Demo)'
note: candidates are:
note: Demo::Demo(Demo&)
note: no known conversion for argument 1 from 'Demo' to 'Demo&'
note: Demo:Demo(int,int)
note: candidate expects 2 arguments, 1 provided
My first question is, I am passing two arguments in the constructor then Why the compiler is not able to distinguish and call the correct constructor?
My second question is, If I change the first line of main to be like this:
Demo c2(5,7);
then It works fine. Why is this so?
Thanks in advance for any help you are able to provide