質問編集履歴
1
書式の改善、タイトルの変更、内容の追加
title
CHANGED
|
@@ -1,1 +1,1 @@
|
|
|
1
|
-
インスタンスが生成できない
|
|
1
|
+
c++ インスタンスが生成できない
|
body
CHANGED
|
@@ -6,17 +6,11 @@
|
|
|
6
6
|
例外がスローされました (AlternatelyCircle.exe 内): Microsoft C++ の例外: std::bad_alloc (メモリの場所 0x003EED5C)。
|
|
7
7
|
下記ソースコードのelse(iが奇数の場合)でLineCircleのインスタンスをnewするときに例外が発生します。
|
|
8
8
|
```
|
|
9
|
+
//main.cpp
|
|
10
|
+
#include "Circle.h"
|
|
11
|
+
#include "ColorCircle.h"
|
|
12
|
+
#include "LineCircle.h"
|
|
9
13
|
|
|
10
|
-
### 該当のソースコード
|
|
11
|
-
|
|
12
|
-
```
|
|
13
|
-
ソースコード
|
|
14
|
-
```言語:c++
|
|
15
|
-
#main.cpp
|
|
16
|
-
include "Circle.h"
|
|
17
|
-
include "ColorCircle.h"
|
|
18
|
-
include "LineCircle.h"
|
|
19
|
-
|
|
20
14
|
int main() {
|
|
21
15
|
Circle* circ; // 基本クラスの変数として宣言
|
|
22
16
|
svg svgObj;
|
|
@@ -26,7 +20,7 @@
|
|
|
26
20
|
if ((i % 2) == 0) { // i が偶数の場合
|
|
27
21
|
circ = new ColorCircle(20 * (i + 1), 20, 10, "green");
|
|
28
22
|
}
|
|
29
|
-
else {// i が奇数の場合
|
|
23
|
+
else {// i が奇数の場合 ここでエラー
|
|
30
24
|
circ = new LineCircle(20 * (i + 1), 20, 10, "red", 3);
|
|
31
25
|
}
|
|
32
26
|
circ->draw(&svgObj); // ColorCircle と LineCircle が交互に呼び出される!
|
|
@@ -38,10 +32,30 @@
|
|
|
38
32
|
|
|
39
33
|
svgObj.close();
|
|
40
34
|
}
|
|
35
|
+
``````
|
|
36
|
+
```c++
|
|
37
|
+
//LineCircle.h
|
|
38
|
+
#pragma once
|
|
39
|
+
#include "ColorCircle.h"
|
|
40
|
+
using namespace std;
|
|
41
41
|
|
|
42
|
+
class LineCircle : public ColorCircle {
|
|
43
|
+
protected:
|
|
44
|
+
string LineColor;
|
|
45
|
+
int width;
|
|
46
|
+
public:
|
|
47
|
+
LineCircle();
|
|
48
|
+
LineCircle(int cx, int cy, int r, string c1, int w);
|
|
49
|
+
virtual void draw(svg*);
|
|
50
|
+
void setLineColor(string c1);
|
|
51
|
+
void setwidth(int w);
|
|
52
|
+
};
|
|
42
53
|
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
```
|
|
43
|
-
|
|
57
|
+
//LinceCircle.cpp
|
|
44
|
-
include"LineCircle.h"
|
|
58
|
+
#include"LineCircle.h"
|
|
45
59
|
using namespace std;
|
|
46
60
|
|
|
47
61
|
LineCircle::LineCircle() : ColorCircle() {
|
|
@@ -67,10 +81,31 @@
|
|
|
67
81
|
}
|
|
68
82
|
|
|
69
83
|
|
|
84
|
+
|
|
85
|
+
``````
|
|
86
|
+
|
|
87
|
+
```
|
|
70
|
-
|
|
88
|
+
//ColorCircle.h
|
|
89
|
+
#pragma once
|
|
71
|
-
include
|
|
90
|
+
#include"Circle.h"
|
|
72
91
|
using namespace std;
|
|
73
92
|
|
|
93
|
+
class ColorCircle : public Circle {
|
|
94
|
+
protected:
|
|
95
|
+
string color; //描画色
|
|
96
|
+
public:
|
|
97
|
+
ColorCircle(); //コンストラクタ
|
|
98
|
+
ColorCircle(int cx, int cy, int r, string c); //引数ありコンストラクタ
|
|
99
|
+
|
|
100
|
+
virtual void draw(svg*); //Circleクラスのdrawをオーバーライド
|
|
101
|
+
void setColor(string c); //色の設定
|
|
102
|
+
};
|
|
103
|
+
```
|
|
104
|
+
```
|
|
105
|
+
//ColorCircle.cpp
|
|
106
|
+
#include "ColorCircle.h"
|
|
107
|
+
using namespace std;
|
|
108
|
+
|
|
74
109
|
ColorCircle::ColorCircle() : Circle() {
|
|
75
110
|
color = "black";
|
|
76
111
|
}
|
|
@@ -88,11 +123,37 @@
|
|
|
88
123
|
}
|
|
89
124
|
|
|
90
125
|
|
|
91
|
-
#Circle.cpp
|
|
92
|
-
include"Circle.h"
|
|
93
126
|
|
|
127
|
+
``````
|
|
128
|
+
```
|
|
129
|
+
//Circle.h
|
|
130
|
+
#pragma once
|
|
131
|
+
#include<iostream>
|
|
132
|
+
#include"svg.h"
|
|
94
133
|
|
|
134
|
+
class Circle {
|
|
135
|
+
protected:
|
|
136
|
+
int x, y;
|
|
137
|
+
int rad;
|
|
95
138
|
|
|
139
|
+
public:
|
|
140
|
+
Circle();
|
|
141
|
+
Circle(int cx, int cy, int r = 10);
|
|
142
|
+
void setPosition(int x, int y);
|
|
143
|
+
void setRadius(int rad);
|
|
144
|
+
int getRadius();
|
|
145
|
+
void getPosition(int xy[]);
|
|
146
|
+
virtual void draw(svg*) = 0;
|
|
147
|
+
//bool checkOverlap(Circle circ);
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
```
|
|
151
|
+
``````
|
|
152
|
+
//Circle.cpp
|
|
153
|
+
#include"Circle.h"
|
|
154
|
+
#include<cmath>
|
|
155
|
+
|
|
156
|
+
|
|
96
157
|
Circle::Circle() { //引数を指定しないコンストラクタ
|
|
97
158
|
x = y = 0;
|
|
98
159
|
rad = 10;
|
|
@@ -126,13 +187,77 @@
|
|
|
126
187
|
svgObj->drawCircle(x, y, rad, "black");
|
|
127
188
|
}
|
|
128
189
|
|
|
190
|
+
/*bool Circle::checkOverlap(Circle circ) {
|
|
191
|
+
int txy1[2];
|
|
192
|
+
int txy2[2];
|
|
193
|
+
int trad;
|
|
194
|
+
double d;
|
|
129
195
|
|
|
196
|
+
trad = circ.getRadius() + this->getRadius(); //2つの半径の和
|
|
197
|
+
circ.getPosition(txy1);
|
|
198
|
+
this->getPosition(txy2);
|
|
199
|
+
//中心の距離
|
|
200
|
+
d = sqrt((txy1[0] - txy2[0]) * (txy1[0] - txy2[0]) + (txy1[1] - txy2[1]) * ((txy1[1] - txy2[1])));
|
|
130
201
|
|
|
202
|
+
if (d > trad) {
|
|
203
|
+
return false;
|
|
204
|
+
}
|
|
205
|
+
else {
|
|
206
|
+
return true;
|
|
207
|
+
}
|
|
131
208
|
|
|
209
|
+
}*/
|
|
210
|
+
`````````
|
|
211
|
+
```
|
|
212
|
+
//svg.h
|
|
213
|
+
#pragma once
|
|
214
|
+
#include<fstream>
|
|
215
|
+
#include<string>
|
|
216
|
+
using namespace std;
|
|
132
217
|
|
|
218
|
+
class svg {
|
|
219
|
+
private:
|
|
220
|
+
ofstream ofs;
|
|
221
|
+
public:
|
|
222
|
+
void open(string filename, int width, int height);
|
|
223
|
+
void close();
|
|
224
|
+
void drawCircle(int x, int y, int rad, string color = "black", int width = 0);
|
|
133
225
|
|
|
226
|
+
};
|
|
134
227
|
|
|
135
228
|
|
|
229
|
+
`````````
|
|
230
|
+
```
|
|
231
|
+
//svg.cpp
|
|
232
|
+
#include"svg.h"
|
|
233
|
+
|
|
234
|
+
void svg::open(string filename, int width, int height) {
|
|
235
|
+
ofs.open(filename);
|
|
236
|
+
ofs << "<svg width='" << width << "' height='" << height << "' >" << endl;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
void svg::close() {
|
|
240
|
+
ofs << "</svg>";
|
|
241
|
+
ofs.close();
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
void svg::drawCircle(int x, int y, int rad, std::string color, int width) {
|
|
245
|
+
if (width > 0) // 線幅が指定されていれば輪郭線を描画する (draw only contour for non-zero width)
|
|
246
|
+
ofs << "<circle cx='" << x << "' cy='" << y << "' r='" << rad
|
|
247
|
+
<< "' stroke='" << color << "' stroke-width='" << width << "' fill='none' />";
|
|
248
|
+
else // 線幅が指定されていないか、値が0以下であれば、円の内部を塗りつぶす (fill inside circle)
|
|
249
|
+
ofs << "<circle cx='" << x << "' cy='" << y << "' r='" << rad
|
|
250
|
+
<< "' fill='" << color << "' />";
|
|
251
|
+
ofs << std::endl; // 改行
|
|
252
|
+
}
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
|
|
136
261
|
### 試したこと
|
|
137
262
|
|
|
138
263
|
親の呼び出し方が間違っていないか確かめたのですがわかりませんでした。
|