質問編集履歴

2

内容の更新

2019/07/28 14:20

投稿

amber_snob
amber_snob

スコア30

test CHANGED
File without changes
test CHANGED
@@ -1,205 +1,205 @@
1
1
  ```c++
2
+
3
+ #include <iostream>
4
+
5
+ #include <string>
6
+
7
+ #include "Shape.h"
8
+
9
+ #include "Circle.h"
10
+
11
+ #include "Rect.h"
12
+
13
+
14
+
15
+ int main(){
16
+
17
+ Shape** shapes = new Shape*[3];
18
+
19
+ Circle c1(0,0,2); //純粋仮想関数を含むオブジェクトは生成できない
20
+
21
+ Circle c2(1,1,5);
22
+
23
+ Rect r1(5,5,8,4);
24
+
25
+ shapes[0] = &c1;
26
+
27
+ shapes[1] = &c2;
28
+
29
+ shapes[2] = &r1;
30
+
31
+ for(int i=0; i<3; i++){
32
+
33
+ std::cout << shapes[i]->area() << std::endl;
34
+
35
+ }
36
+
37
+ delete[] shapes;
38
+
39
+ }
40
+
41
+ ```
42
+
43
+
44
+
45
+ 上のソースコードを実行したところ、
46
+
47
+ >
48
+
49
+ error: cannot declare variable 'c1' to be of abstract type 'Circle'
50
+
51
+ error: cannot declare variable 'c2' to be of abstract type 'Circle'
52
+
53
+ error: cannot declare variable 'r1' to be of abstract type 'Rect'
54
+
55
+
56
+
57
+ のようなエラーがでました。
58
+
59
+
60
+
61
+ 調べると、「純粋仮想関数」を持つクラスのオブジェクトは作れないようで、
62
+
63
+ virtualがついているメソッドを探したのですが見つかりませんでした。
64
+
65
+
66
+
67
+ 原因が分からないので教えていただきたいです。
68
+
69
+ (ちなみに、Rect.h, Circle.hにもエラーが出ています。)
70
+
71
+
72
+
73
+ また、Shape.h, Circle.h, Rect.hは次のようになります。
74
+
75
+
76
+
77
+ ```c++
78
+
79
+ #ifndef SHAPE_H_
80
+
81
+ #define SHAPE_H_
82
+
83
+
2
84
 
3
85
  #include <sstream>
4
86
 
5
87
  #include <string>
6
88
 
89
+
90
+
91
+ class Shape{
92
+
93
+ public:
94
+
95
+ virtual ~Shape() = 0;
96
+
97
+ virtual int area() const = 0;
98
+
99
+ //virtual std::String toString() const = 0;
100
+
101
+ };
102
+
103
+
104
+
105
+ inline Shape::~Shape() {};
106
+
107
+
108
+
109
+ /*
110
+
111
+ inline std::ostream& operator<<(std::ostream& s, const Shape& shape){
112
+
113
+ return s << shape.toString();
114
+
115
+ }
116
+
117
+ */
118
+
119
+
120
+
121
+ #endif /* SHAPE_H_ */
122
+
123
+ ```
124
+
125
+
126
+
127
+ ```c++
128
+
129
+ #ifndef CIRCLE_H_
130
+
131
+ #define CIRCLE_H_
132
+
133
+
134
+
7
135
  #include "Shape.h"
8
136
 
9
- #include "Circle.h"
10
-
11
- #include "Rect.h"
137
+ #include <math.h>
138
+
139
+
140
+
12
-
141
+ class Circle: public Shape{
142
+
13
-
143
+ protected: //継承先で参照するため
14
-
144
+
15
- int main(){
145
+ int x, y, r;
16
-
17
- Shape** shapes = new Shape*[3];
146
+
18
-
19
- Circle c1(0,0,2); //純粋仮想関数を含むオブジェクトは生成できない
20
-
21
- Circle c2(1,1,5);
147
+ public:
22
-
23
- Rect r1(5,5,8,4);
148
+
24
-
25
- Shapes[0] = &c1;
26
-
27
- Shapes[1] = &c2;
28
-
29
- Shapes[2] = &r1;
30
-
31
- for(int i=0; i<3; i++){
149
+ Circle(int xx, int yy, int rr){
32
-
150
+
33
- std::cout << shapes[i]->area() << std::endl;
151
+ x(xx), y(yy), r(rr) {}
34
152
 
35
153
  }
36
154
 
37
- delete[] shapes;
155
+ ~Circle() {}
156
+
38
-
157
+ int area() { return r * r * M_PI; }
158
+
39
- }
159
+ };
160
+
161
+
162
+
40
-
163
+ #endif /* CIRCLE_H_ */
164
+
41
- ```
165
+ ```
42
-
43
-
44
-
45
- 上のソースコードを実行したところ、
166
+
46
-
47
- >
167
+
48
-
49
- error: cannot declare variable 'c1' to be of abstract type 'Circle'
168
+
50
-
51
- error: cannot declare variable 'c2' to be of abstract type 'Circle'
52
-
53
- error: cannot declare variable 'r1' to be of abstract type 'Rect'
54
-
55
-
56
-
57
- のようなエラーがでました。
58
-
59
-
60
-
61
- 調べると、「純粋仮想関数」を持つクラスのオブジェクトは作れないようで、
62
-
63
- virtualがついているメソッドを探したのですが見つかりませんでした。
64
-
65
-
66
-
67
- 原因が分からないので教えていただきたいです。
68
-
69
- (ちなみに、Rect.h, Circle.hにもエラーが出ています。)
70
-
71
-
72
-
73
- また、Shape.h, Circle.h, Rect.hは次のようになります。
74
-
75
-
76
-
77
- ```c++
169
+ ```c++
78
-
170
+
79
- #ifndef SHAPE_H_
171
+ #ifndef RECT_H_
80
-
172
+
81
- #define SHAPE_H_
173
+ #define RECT_H_
82
-
83
-
84
-
174
+
175
+
176
+
85
- #include <sstream>
177
+ #include "Shape.h"
86
-
87
- #include <string>
178
+
88
-
89
-
90
-
179
+
180
+
91
- class Shape{
181
+ class Rect: public Shape{
182
+
183
+ protected: //継承先で参照するため
184
+
185
+ int x, y, w, h;
92
186
 
93
187
  public:
94
188
 
189
+ Rect(int xx, int yy, int ww, int hh){
190
+
95
- virtual ~Shape() = 0;
191
+ x(xx), y(yy), w(ww), h(hh) {}
192
+
96
-
193
+ }
194
+
195
+ ~Rect() {}
196
+
97
- virtual int area() const = 0;
197
+ int area() { return w * h; }
98
-
99
- //virtual std::String toString() const = 0;
100
198
 
101
199
  };
102
200
 
103
201
 
104
202
 
105
- inline Shape::~Shape() {};
106
-
107
-
108
-
109
- /*
110
-
111
- inline std::ostream& operator<<(std::ostream& s, const Shape& shape){
112
-
113
- return s << shape.toString();
114
-
115
- }
116
-
117
- */
118
-
119
-
120
-
121
- #endif /* SHAPE_H_ */
122
-
123
- ```
124
-
125
-
126
-
127
- ```c++
128
-
129
- #ifndef CIRCLE_H_
130
-
131
- #define CIRCLE_H_
132
-
133
-
134
-
135
- #include "Shape.h"
136
-
137
- #include <math.h>
138
-
139
-
140
-
141
- class Circle: public Shape{
142
-
143
- protected: //継承先で参照するため
144
-
145
- int x, y, r;
146
-
147
- public:
148
-
149
- Circle(int xx, int yy, int rr){
150
-
151
- x(xx), y(yy), r(rr) {}
152
-
153
- }
154
-
155
- ~Circle() {}
156
-
157
- int area() { return r * r * M_PI; }
158
-
159
- };
160
-
161
-
162
-
163
- #endif /* CIRCLE_H_ */
164
-
165
- ```
166
-
167
-
168
-
169
- ```c++
170
-
171
- #ifndef RECT_H_
172
-
173
- #define RECT_H_
174
-
175
-
176
-
177
- #include "Shape.h"
178
-
179
-
180
-
181
- class Rect: public Shape{
182
-
183
- protected: //継承先で参照するため
184
-
185
- int x, y, w, h;
186
-
187
- public:
188
-
189
- Rect(int xx, int yy, int ww, int hh){
190
-
191
- x(xx), y(yy), w(ww), h(hh) {}
192
-
193
- }
194
-
195
- ~Rect() {}
196
-
197
- int area() { return w * h; }
198
-
199
- };
200
-
201
-
202
-
203
203
  #endif /* RECT_H_ */
204
204
 
205
205
  ```

1

内容の追記

2019/07/28 14:20

投稿

amber_snob
amber_snob

スコア30

test CHANGED
File without changes
test CHANGED
@@ -66,6 +66,8 @@
66
66
 
67
67
  原因が分からないので教えていただきたいです。
68
68
 
69
+ (ちなみに、Rect.h, Circle.hにもエラーが出ています。)
70
+
69
71
 
70
72
 
71
73
  また、Shape.h, Circle.h, Rect.hは次のようになります。