私は2つのクラスを持って、1それは抽象クラスからのパラメータです継承したクラス:2つのオーバーロードされたコンストラクタを持っているオブジェクトを別のクラスコンストラクタのパラメータとして渡すにはどうすればよいですか?
class Child : public Base {
public:
Child(string s, int i,) : Base(s, i){};
... // methods
};
と別のは、一つは通常のパラメータを使用し、別のは、同じパラメータを取得しますが、最初のクラスから既存のオブジェクト:
ヘッダファイル:
class Other {
private:
string s;
int i;
Child o;
public:
Other(string str, int num);
Other(Child ob);
};
CPPファイル:
Other :: Other(string str, int num) : s(str), i(num) {/* this is where the error happens*/};
Other :: Other(Child ob) : o(ob) {
};
しかし、私はマークされた場所でエラーを取得するコンパイルしようと、「C2512 『その他』:いいえ、適切なデフォルトコンストラクタが利用できる」
を問題に何ができますか? Other
はChild
メンバーを持っているので、これが発生し
Other :: Other(string str, int num) : s(str), i(num), o(str, num) {}
'Child'のデフォルトのコンストラクタは何ですか? –
はありません。私は既存の値で 'Child'オブジェクトを初期化する必要があります – Yokubasu
メンバー初期化子リストでそれを行うだけですか? http://stackoverflow.com/questions/1711990/what-is-this-weird-colon-member-syntax-in-the-constructor –