Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is wrong in this template definition?

Tags:

c++

templates

template <int N>
class myarray {
    typedef int Bitmap;
public:
    static Bitmap data[N];
};

template <int N> myarray<N>::Bitmap myarray<N>::data[N];

error: expected constructor, destructor, or type conversion before ‘myarray’

like image 308
Martin Avatar asked Dec 20 '11 01:12

Martin


People also ask

What is the concept of template?

A template is a form, mold or pattern used as a guide to make something. Here are some examples of templates: Website design. Creating a document.

Are templates good C++?

If you want to program in C++ there are lots of times when templates are absolutely the best option the language gives you for writing generic, reusable code. On the other hand, just because templates are the best C++ has to offer doesn't mean they're good.

Are templates fast in C++?

Templates are fully evaluated by the compiler, and so they have zero overhead at runtime. Calling Foo<int>() is exactly as efficient as calling FooInt() . So compared to approaches which rely on more work being done at runtime, for example by calling virtual functions, templates can indeed be faster.

Are C++ templates expensive?

Since template instantiation happens at compile time, there is no run-time cost to using templates (as a matter of fact templates are sometimes used to perform certain computations at compile-time to make the program run faster).


1 Answers

You need typename before myarray<N>::Bitmap because it is a dependent type:

template <int N>
class myarray {
    typedef int Bitmap;
public:
    static Bitmap data[N];
};

   template <int N>
   typename myarray<N>::Bitmap myarray<N>::data[N];
// ^^^^^^^^
like image 85
Seth Carnegie Avatar answered Sep 22 '22 10:09

Seth Carnegie