conversion Error?

Why this code returns en error ?



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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99

struct X
{
    int i;

    X(int in)
    {
	cout << "converting int " << in << " to X\n";
	i = in;
    }

    operator+(int in)
    {
	cout << "adding X " << i << " + int " << in << "\n";
	int ret = i + in;
	cout << "op+(X,int) returns " << ret << "\n";
	return ret;
    }
};

struct Y
{
    int i;

    Y(X Xin)
    {
	cout << "converting X " << Xin.i << " to Y\n";
	i = Xin.i;
    }

    operator+(X Xin)
    {
	cout << "adding Y " << i << " to X " <<
	    Xin.i << "\n";
	int ret = i + Xin.i;
	cout << "op+(Y,X) returns " << ret << "\n";
	return ret;
    }

    operator int()
    {
	cout << "converting Y " << i <<
	    " to int\n";
	return i;
    }
};

X operator* (X Xin, Y Yin)
{
    cout << "multiplying X " << Xin.i << " times Y " <<
	Yin.i << "\n";
    X Xout(Xin.i * Yin.i);
    cout << "op*(X, Y) returns " << Xout.i << "\n";
    return Xout;
}

int f(X Xin)
{
    cout << "calling f(X = " << Xin.i << ")\n";
    return Xin.i;
}

X x = 1;
Y y = x;
int i = 2;

main()
{
    int ret = i + 10;
    cout << "i + 10 = " << ret << "\n";

    ret = y + 10;
    cout << "y + 10 = " << ret << "\n";

    ret = y + 10 * y;
    cout << "y + 10 * y = " << ret << "\n";

    ret = x + y + i;
    cout << "x + y + i = " << ret << "\n";

    ret = x * x + i;
    cout << "x * x + i = " << ret << "\n";

    ret = f(7);
    cout << "f(7) = " << ret << "\n";

//  illegal conversion
//  ret = f(y);
//  cout << "f(y) = " << ret << "\n";

    ret = y + y;
    cout << "y + y = " << ret << "\n";

    ret = 106 + y;
    cout << "106 + y = " << ret << "\n";
    return 0;
}

You forgot to specify the return types for operator+ and main.
still does not work


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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

struct X
{
    int i;

    X(int in)
    {
	cout << "converting int " << in << " to X\n";
	i = in;
    }

    int operator+(int in)
    {
	cout << "adding X " << i << " + int " << in << "\n";
	int ret = i + in;
	cout << "op+(X,int) returns " << ret << "\n";
	return ret;
    }
};

struct Y
{
    int i;

    Y(X Xin)
    {
	cout << "converting X " << Xin.i << " to Y\n";
	i = Xin.i;
    }

    int Y::operator+(X Xin)
    {
	cout << "adding Y " << i << " to X " <<
	    Xin.i << "\n";
	int ret = i + Xin.i;
	cout << "op+(Y,X) returns " << ret << "\n";
	return ret;
    }

     operator int()
    {
	cout << "converting Y " << i <<
	    " to int\n";
	return i;
    }
};

X operator* (X Xin, Y Yin)
{
    cout << "multiplying X " << Xin.i << " times Y " <<
	Yin.i << "\n";
    X Xout(Xin.i * Yin.i);
    cout << "op*(X, Y) returns " << Xout.i << "\n";
    return Xout;
}

int f(X Xin)
{
    cout << "calling f(X = " << Xin.i << ")\n";
    return Xin.i;
}

X x = 1;
Y y = x;
int i = 2;

int main()
{
    int ret = i + 10;
    cout << "i + 10 = " << ret << "\n";

    ret = y + 10;
    cout << "y + 10 = " << ret << "\n";

    ret = y + 10 * y;
    cout << "y + 10 * y = " << ret << "\n";

    ret = x + y + i;
    cout << "x + y + i = " << ret << "\n";

    ret = x * x + i;
    cout << "x * x + i = " << ret << "\n";

    ret = f(7);
    cout << "f(7) = " << ret << "\n";

//  illegal conversion
//  ret = f(y);
//  cout << "f(y) = " << ret << "\n";

    ret = y + y;
    cout << "y + y = " << ret << "\n";

    ret = 106 + y;
    cout << "106 + y = " << ret << "\n";
    return 0;
}
You have an ambiguous overload, meaning that when you call ret = y + 10 the compiler has 2 options and no way of knowing what you mean.
Do you mean use y as being a int and store the sum in ret, or do you mean do y + 10 and then store y in ret as being a int.

In other words is y a int or object of type Y in this context?

to fix this use ret = (int)y + 10.

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>
using namespace std;

struct X
{
    int i;

    X(int in)
    {
	cout << "converting int " << in << " to X\n";
	i = in;
    }

    int operator+(int in)
    {
	cout << "adding X " << i << " + int " << in << "\n";
	int ret = i + in;
	cout << "op+(X,int) returns " << ret << "\n";
	return ret;
    }
};

struct Y
{
    int i;

    Y(X Xin)
    {
	cout << "converting X " << Xin.i << " to Y\n";
	i = Xin.i;
    }

    int operator+(X Xin)
    {
	cout << "adding Y " << i << " to X " <<
	    Xin.i << "\n";
	int ret = i + Xin.i;
	cout << "op+(Y,X) returns " << ret << "\n";
	return ret;
    }

     operator int()
    {
	cout << "converting Y " << i <<
	    " to int\n";
	return i;
    }
};

X operator* (X Xin, Y Yin)
{
    cout << "multiplying X " << Xin.i << " times Y " <<
	Yin.i << "\n";
    X Xout(Xin.i * Yin.i);
    cout << "op*(X, Y) returns " << Xout.i << "\n";
    return Xout;
}

int f(X Xin)
{
    cout << "calling f(X = " << Xin.i << ")\n";
    return Xin.i;
}

X x = 1;
Y y = x;
int i = 2;

int main()
{
    int ret = i + 10;
    cout << "i + 10 = " << ret << "\n";

    ret = (int)y + 10;
    cout << "y + 10 = " << ret << "\n";

    ret = (int)y + 10 * (int)y;
    cout << "y + 10 * y = " << ret << "\n";

    ret = x + y + i;
    cout << "x + y + i = " << ret << "\n";

    ret = x * x + i;
    cout << "x * x + i = " << ret << "\n";

    ret = f(7);
    cout << "f(7) = " << ret << "\n";

//  illegal conversion
//  ret = f(y);
//  cout << "f(y) = " << ret << "\n";

    ret = y + y;
    cout << "y + y = " << ret << "\n";

    ret = 106 + y;
    cout << "106 + y = " << ret << "\n";
    return 0;
}
Topic archived. No new replies allowed.