KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Here is some code outlining a problem I've been wrestling with. The final problem (as far as g++ is concerned at the moment) is that: "error: 'Foo-T' was not declared in this scope" when performing the Bar::Bar(...) constructor routine. Otherwise, the problem I'm attempting to learn my way through is one of setting base-class member types based on arguments passed to a derived-class constructor using templates. If there were a way to set the base-class member type (T Foo-T) simply by passing arguments to the derived-class constructor, I would prefer that. As of now I can't see a way past using both the template argument and a matching derived-class constructor argument to accomplish this task. Can you spot anything in the following code that I can be doing better to achieve the same goals? I'm rather new to generic-coding and templates. #include <iostream> typedef int a_arg_t; typedef double b_arg_t; typedef std::string foo_arg_t; class TypeA { public: TypeA (); TypeA (a_arg_t a) { /* Do sosmething with the parameter passed in */ } }; class TypeB { public: TypeB (); TypeB (b_arg_t b) { /* typeB's constructor - do something here */ } }; // The base-class with a member-type to be determined by the template argument template <class T> class Foo { public: Foo (const foo_arg_t foo_arg) : _foo_arg(foo_arg) // initialize something here { /* do something for foo */ } T Foo_T; // either a TypeA or a TypeB - TBD foo_arg_t _foo_arg; }; // the derived class that should set the basse-member type (T Foo_T) template <class T> class Bar : public Foo<T> { public: Bar (const foo_arg_t bar_arg, const a_arg_t a_arg) : Foo<T>(bar_arg) // base-class initializer { // the initialization of Foo_T has to be done outside the initializer list because it's not in scsope until here Foo_T = TypeA(a_arg); // if an a_arg_t is passed in, then we set the Fo
Tags (comma-separated)
Save Edits
Cancel