質問編集履歴
1
書き直したコードとエラーを追加しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,28 +1,82 @@
|
|
1
|
+
大幅に書き変えました, すみません.
|
2
|
+
|
3
|
+
|
4
|
+
|
1
|
-
演算子の練習をしているのですが, 以下の
|
5
|
+
演算子の練習をしているのですが, 以下のエラーがでてしまいます.
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
+
|
6
|
-
|
7
|
-
下4つは一つでもいいので教えて頂けると嬉しいです.
|
8
|
-
|
9
|
-
|
7
|
+
どこを直せば良いですか?
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
```
|
12
|
+
|
13
|
+
11th.operator.cpp:32:23: error: assigning to 'double' from incompatible type 'A'
|
14
|
+
|
15
|
+
a3.x = (*this)*s;
|
16
|
+
|
17
|
+
~~~~~~~^~
|
18
|
+
|
19
|
+
11th.operator.cpp:45:19: error: member reference base type 'double' is not a structure or
|
20
|
+
|
21
|
+
union
|
22
|
+
|
23
|
+
x += a.x;
|
24
|
+
|
25
|
+
```
|
10
26
|
|
11
27
|
|
12
28
|
|
13
29
|
```c++
|
14
30
|
|
31
|
+
#include <iostream>
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
using namespace std;
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
class A{
|
40
|
+
|
41
|
+
double x;
|
42
|
+
|
43
|
+
public:
|
44
|
+
|
45
|
+
A operator+(A a) // やりたいこと: (*this) + a;
|
46
|
+
|
47
|
+
{
|
48
|
+
|
49
|
+
A a1;
|
50
|
+
|
51
|
+
a1.x = (*this).x + a.x;
|
52
|
+
|
53
|
+
return a1;
|
54
|
+
|
55
|
+
}
|
56
|
+
|
57
|
+
A operator-(A a) // やりたいこと: (*this) - a;
|
58
|
+
|
59
|
+
{
|
60
|
+
|
61
|
+
A a2;
|
62
|
+
|
63
|
+
a2.x = (*this).x - a.x;
|
64
|
+
|
65
|
+
return a2;
|
66
|
+
|
67
|
+
}
|
68
|
+
|
15
69
|
bool operator>(A a) // やりたいこと: (*this) > a;
|
16
70
|
|
17
71
|
{
|
18
72
|
|
19
|
-
|
73
|
+
return (*this).x > a.x;
|
20
74
|
|
21
75
|
}
|
22
76
|
|
23
77
|
bool operator==(A a){ // やりたいこと: (*this) == a;
|
24
78
|
|
25
|
-
|
79
|
+
return (*this).x == a.x;
|
26
80
|
|
27
81
|
}
|
28
82
|
|
@@ -30,26 +84,152 @@
|
|
30
84
|
|
31
85
|
{
|
32
86
|
|
87
|
+
A a3;
|
88
|
+
|
33
|
-
|
89
|
+
a3.x = (*this)*s;
|
90
|
+
|
91
|
+
return a3;
|
34
92
|
|
35
93
|
}
|
36
94
|
|
37
95
|
double& operator[](int i){ // やりたいこと: (*this).x = some thing;
|
38
96
|
|
39
|
-
|
97
|
+
return (*this).x;
|
40
98
|
|
41
99
|
}
|
42
100
|
|
101
|
+
|
102
|
+
|
43
|
-
A operator=(A a){ // やりたいこと: (*this) = a;
|
103
|
+
A &operator=(const A &a){ // やりたいこと: (*this) = a;
|
104
|
+
|
44
|
-
|
105
|
+
(*this).x = a.x;
|
106
|
+
|
45
|
-
|
107
|
+
return (*this);
|
46
108
|
|
47
109
|
}
|
48
110
|
|
49
111
|
A& operator<<(double a){ // やりたいこと: (*this) << a;
|
50
112
|
|
113
|
+
|
114
|
+
|
51
|
-
|
115
|
+
x += a.x;
|
116
|
+
|
117
|
+
return (*this);
|
52
118
|
|
53
119
|
}
|
54
120
|
|
121
|
+
|
122
|
+
|
123
|
+
};
|
124
|
+
|
125
|
+
// 以下に正しい出力を示しておく.
|
126
|
+
|
127
|
+
// 1, 2
|
128
|
+
|
129
|
+
// 1 < 2
|
130
|
+
|
131
|
+
// 1 != 2
|
132
|
+
|
133
|
+
// 3, 2
|
134
|
+
|
135
|
+
// 1, 2
|
136
|
+
|
137
|
+
// 2, 2
|
138
|
+
|
139
|
+
// 4
|
140
|
+
|
141
|
+
// 8
|
142
|
+
|
143
|
+
// 10
|
144
|
+
|
145
|
+
// 16
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
int main(){
|
150
|
+
|
151
|
+
A a1, a2;
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
a1[0] = 1;
|
156
|
+
|
157
|
+
a2[-1] = 2;
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
cout << a1[-9999] << ", " << a2[0] << "\n";
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
if(a1 > a2){
|
166
|
+
|
167
|
+
cout << a1[1651651] << " > " << a2[0] << "\n";
|
168
|
+
|
169
|
+
}else{
|
170
|
+
|
171
|
+
cout << a1[0] << " < " << a2[0] << "\n";
|
172
|
+
|
173
|
+
}
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
if(a1 == a2){
|
178
|
+
|
179
|
+
cout << a1[0] << " == " << a2[0] << "\n";
|
180
|
+
|
181
|
+
}else{
|
182
|
+
|
183
|
+
cout << a1[0] << " != " << a2[0] << "\n";
|
184
|
+
|
185
|
+
}
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
a1 = a1 + a2;
|
190
|
+
|
191
|
+
cout << a1[0] << ", " << a2[0] << "\n";
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
a1 = a1 - a2;
|
196
|
+
|
197
|
+
cout << a1[0] << ", " << a2[0] << "\n";
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
a1 = a2;
|
202
|
+
|
203
|
+
cout << a1[0] << ", " << a2[0] << "\n";
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
a1 = a1*2;
|
208
|
+
|
209
|
+
cout << a1[0] << "\n";
|
210
|
+
|
211
|
+
a1 = a1 + a2*2;
|
212
|
+
|
213
|
+
cout << a1[0] << "\n";
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
a1 << 2;
|
218
|
+
|
219
|
+
cout << a1[0] << "\n";
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
a1 << 1 << 2 << 3;
|
224
|
+
|
225
|
+
cout << a1[0] << "\n";
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
return 0;
|
232
|
+
|
233
|
+
}
|
234
|
+
|
55
235
|
```
|