Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'X is not a template' error

Tags:

c++

gcc

templates

I'm having trouble declaring a template class. I've tried an number of ill-readable and non-sensical combinations.

template <class C, class M >
class BlockCipherGenerator : public KeyGenerator
{
  ...
  private:
      M < C > m_cipher;
};

And

template <class C, class M >
class BlockCipherGenerator : public KeyGenerator
{
  typedef typename C::value_type CIPHER;
  typedef typename M::value_type MODE;
  private:
      MODE < CIPHER > m_cipher;
};
like image 425
jww Avatar asked Aug 07 '11 01:08

jww


People also ask

What does template <> mean in C ++?

A template is a simple yet very powerful tool in C++. The simple idea is to pass data type as a parameter so that we don't need to write the same code for different data types. For example, a software company may need to sort() for different data types.

How do I force a template instantiation?

To instantiate a template function explicitly, follow the template keyword by a declaration (not definition) for the function, with the function identifier followed by the template arguments. template float twice<float>(float original); Template arguments may be omitted when the compiler can infer them.

How do you declare a template in C++?

Class Template DeclarationA class template starts with the keyword template followed by template parameter(s) inside <> which is followed by the class declaration. template <class T> class className { private: T var; ... .. ... public: T functionName(T arg); ... .. ... };

Can you template a constructor?

As long as you are satisfied with automatic type inference, you can use a template constructor (of a non-template class). @updogliu: Absolutely. But, the question is asking about "a template constructor with no arguments" If there are no function arguments, no template arguments may be deduced.


1 Answers

It's what it says.

Your template parameter list says that M is a class, not a template.

If you say that it's a class template, then everything's fine:

template <class C, template <class C> class M>
class BlockCipherGenerator : public KeyGenerator
{
      M<C> m_cipher;
};

Remember, something like std::vector is not a class, but a class template. Something like std::vector<int> is a class (type).

like image 105
Lightness Races in Orbit Avatar answered Oct 03 '22 18:10

Lightness Races in Orbit