-1

I want to overwrite a type with a typedef. The rationale for doing this is that one of my class has lot of templates and i want to replace all calls to that class with the templatized class (so that within another class Achild means Achild<T>. However, i get an error.

template <typename T>
   class Achild
   {
   public:
     Achild<T>() = default;


   };

  class Foo
{
        typedef Achild<int> Achild;
        public:
                Foo() = default;

};

int main()
{
        auto foo = new Foo();
}

I get the following error:

new_test.cpp:12:22: error: declaration of ‘typedef class Achild<int> Foo::Achild’ [-fpermissive]
  typedef Achild<int> Achild;
                      ^~~~~~
new_test.cpp:2:10: error: changes meaning of ‘Achild’ from ‘class Achild<int>’ [-fpermissive]
    class Achild

How can i get this to work? The example is only for sample and my reasons for doing this is also related to how i have to get this to work with an existing codebase.

Captain Jack sparrow
  • 959
  • 1
  • 13
  • 28

1 Answers1

1

You are aliasing the type with itself (Achild already a type since you declared it in your class). You should instead do:

using Child = Achild<int>;

Note that you also should use the keyword using instead of typedef since it is the equivalent (and more robust) in C++. See What is the difference between 'typedef' and 'using' in C++11?.

Thomas Caissard
  • 846
  • 4
  • 10