default/copy ctor

Hi.Why do I get errors on:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
include <iostream>
using namespace std;
#include <conio.h>
int i = 0;
struct widget{
widget(){cout << "default ctor called" << ++i << endl;}
widget(const widget & _cpy = s);
widget(const widget & _cpy){cout << "copy ctor called" << ++i << endl;}
}s,z;

widget::widget(const widget & _cpy = s){cout << "default ctor called" << ++i << endl;}



widget f(widget u)
{
       widget V(u);
       widget W = V;
       return W;
}
int main()
{
    
    widget x;
    widget y = f(f(x));

    
    
    
    while(1);

    return 0;
}


?Thanks in advance!
Hello hoosh,

First of all, are you programming on a mac or pc? Also, could you please describe in more detail what the errors are so I can help you better. Possibly include line numbers that your compliler gives you?

Good Luck As Always,

Prototype
Hello.
I'm programming on a win xp 32-bit PC.

I changed the class definition to:
1
2
3
4
5
6
7
8
9
#include <iostream>
using namespace std;
#include <conio.h>
int i = 0;
struct widget{
widget(){cout << "default ctor called" << ++i << endl;}
widget(const widget & _cpy);
widget(const widget & _cpy){cout << "copy ctor called" << ++i << endl;}
}s,z;


Here are the errors:

8 C:\Documents and Settings\soheil\My Documents\testsuit\91s8.cpp `widget::widget(const widget&)' and `widget::widget(const widget&)' cannot be overloaded

C:\Documents and Settings\soheil\My Documents\testsuit\91s8.cpp In function `int main()':

24 C:\Documents and Settings\soheil\My Documents\testsuit\91s8.cpp call of overloaded `widget()' is ambiguous

note C:\Documents and Settings\soheil\My Documents\testsuit\91s8.cpp:11 candidates are: widget::widget(const widget&)

note C:\Documents and Settings\soheil\My Documents\testsuit\91s8.cpp:11 widget::widget()
You can't declare more than one copy constructor.

1
2
3
4
5
6
struct widget
{
    widget(){cout << "default ctor called" << ++i << endl;}
    // widget(const widget & _cpy ); // *** duplicate declaration
    widget(const widget & _cpy ){cout << "copy ctor called" << ++i << endl;}
}s,z;

I thought that the constructor that takes no argument(i.e, can have many defaulted parameters) is called a default argument.....................
Topic archived. No new replies allowed.