正常に動作するコードがありますが、例外処理を追加するとブレークします。私は初心者ですし、ここでこのコードは動作するコードcatchブロックの導入後に予期せずプログラムが終了する
です(簡潔のために警備員が示されていない含める):
plane.hで:
#include "vector.h" //which, like point.h, inherits from orderedpair.h
class Plane {
public:
Plane(const std::string &label);
Plane(const Plane &);
virtual ~Plane();
void insert(const OrderedPair &);
void label(const std::string & label);
std::string label() const {return _label;}
protected:
std::string _label;
bool operator==(const Plane &rhs) const {return label() == rhs.label();}
bool operator<(const Plane & rhs) const {return label() < rhs.label();}
};
plane.cppで
#include <set>
Plane::Plane(const std::string &label) : _label(label) {
getUniverse().insert(this);
}
/*GLOBAL OBJECTS*/
extern std::set<Plane *>& getUniverse() {
static std::set<Plane *>universe;
return universe;
}
extern Plane& getDefaultPlane() {
static Plane Default("Default");
return Default;
}
void printworld() {
for (auto i : getUniverse())
std::cout << (*getUniverse().find(i))->label() << std::endl;
}
とメインで:
//MinGW32 4.9 Code::Blocks 16.01 Windows Vista(!!) 64bit -std=C++11
#include <iostream>
#include "vector.h" //includes point.h which includes orderedpair.h
#include "plane.h"
using namespace std;
namespace {
struct Initializer {;
Initializer();
};
}
int main() {
static Initializer init;
Point A("A", 1, 1);
Plane P("P");
Plane duplicate("Default");
printworld();
return 0;
}
Initializer::Initializer()
{
// Trigger construction of the default Plane
getDefaultPlane();
}
は、それから私はplane.hで例外を含め、先頭に次の行を追加します。
class E : public std::exception {
const char *msg = nullptr;
public:
E(const char *m) throw() : msg(m) {}
const char *what() const throw() {return msg;}
};
const E DuplicateObj("Object already exists!\n");
それは問題なく、コンパイルし、実行します。 は、それから私は(必ずラベルが以前に使用されていないにするために)、このように平面のコンストラクタを変更:
Plane::Plane(const std::string &label) : _label(label) {
for (auto i : getUniverse()) {
if ((*getUniverse().find(i))->label() == _label) throw DuplicateObj;
}
getUniverse().insert(this);
}
とメイン内の重複平面のインスタンス化をコメントアウト(スローをトリガーする避けるため)。コンパイルして実行しても問題ありません。
その後、私がメインでtry/catchブロック内のオブジェクトのインスタンス化の行を折り返す:
try {
Point A("A", 1, 1);
Plane P("P");
//Plane duplicate("Default");
}
catch (const E& e) {
cerr << e.what() << endl;
}
catch (const exception &e) {
cerr << e.what() << endl;
}
これは、コンパイルしますが、以下の結果にクラッシュ:
Process returned -1073741819 (0xC0000005) execution time : 9.537 s
Press any key to continue.
プライベートのデフォルトコンストラクタは必要ありません。別のコンストラクタを指定すると、デフォルトのコンストラクタは無効になります。 –
例外をスローするコードは表示されません。 –
理想的には、完全でコンパイル可能なものを投稿してください。 – kec