私はC++の初心者ですので、助けてください。 テンプレートクラスをコンストラクタのパラメータとして使用することはできません。 xcodeに 'が表示されます。' Work 'の初期化に一致するコンストラクタがありません'エラー。 以下のソースコード全体が、これを修正できますか?C++のテンプレート<基本クラスと派生クラス>
#include <iostream>
class Base {
public:
virtual void hello_world() const {
printf("Base::hello_world()\n");
};
};
class Derived : public Base {
public:
void hello_world() const {
printf("Derived::hello_world()\n");
};
};
template<class T>
class Templ {
public:
Templ(const T &t) : _value(t) {}
const T& getValue() const{
return _value;
}
private:
const T &_value;
};
class Work {
public:
Work(const Templ<Base*> &base) : mBase(base) {}
void working() {
mBase.getValue()->hello_world();
}
private:
const Templ<Base*> &mBase;
};
int main(int argc, const char * argv[]) {
Templ<Base*> base(new Base());
//OK
Work w_base(base);
Templ<Derived*> derived(new Derived());
//error: No matching constructor for initialization of 'Work'
Work w_derived(derived);
return 0;
}
これらの参考資料は怖いです。 'Base *'ポインタ 'base._value'の生涯は何を指していますか? – aschepler