Strange error in VS

Hi guys,
this is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Date{
private:
	int day;
	int m;
	int y;

public:
	Date(int day,int m,int y){ //constructor
		this->day=day;
		this->m=m;
		this->y=y;
	}
	Date(Date time){ //copy constructor, ERROR
		this->day=time.day;
		this->m=time.m;
		this->y=time.y;
	}
};


and I get this error:
error C2652: 'Date' : illegal copy constructor: first parameter must not be a 'Date'.

Isn't the whole point of copy constructor is get it's own class parameter?

Thanks!
Try to change it to:
1
2
3
4
5
Date(const Date& time){ //copy constructor, ERROR
		this->day=time.day;
		this->m=time.m;
		this->y=time.y;
	}


Edit: 1st Line deleted.
Last edited on
According to the C++ STandard

Section 12.8 Copying and moving class objects

2 A non-template constructor for class X is a copy constructor if its first parameter is of type X&, const X&, volatile X& or const volatile X&, and either there are no other parameters or else all other parameters have default arguments

6 A declaration of a constructor for a class X is ill-formed if its first parameter is of type (optionally cv-qualified) X and either there are no other parameters or else all other parameters have default arguments
Isn't the whole point of copy constructor is get it's own class parameter?


The purpose of a copy constructor is to define the way you copy an object. So, requiring a copy to be made to call the copy constructor... not so good.
Last edited on
Topic archived. No new replies allowed.