Hi guys,

The following code doesn't compile, and I've been banging my head why. Some header code:

template <typename T, typename LESS_THAN> 
class RestrictedSet 
{
private :
  int _size;
  int _max_size;
  set<T, LESS_THAN> _S;
  typedef typename std::set<T, LESS_THAN>::iterator tIt;

public :
  RestrictedSet (int max_size);
  RestrictedSet (RestrictedSet<T, LESS_THAN> &other);
  ~RestrictedSet ();

  void insert (T &elem);
  T pop_best ();   
  void drop_worst ();
  void show ();
};

And some .cpp code:

template <typename T, typename LESS_THAN>
void RestrictedSet::insert (const T elem) 
{
  _S.insert (elem);
  if (++_size > _max_size) drop_worst(); 
}

The problem must be somewhere in the template part, I get the error:

agenda.cpp:28: error: ‘template<class T> class RestrictedSet’ used without template parameters
agenda.cpp:28: error: variable or field ‘insert’ declared void
agenda.cpp:28: error: expected nested-name-specifier before ‘T’
agenda.cpp:28: error: expected ‘(’ before ‘T’

I also tried variants like:

void <T, LESS_THAN> RestrictedSet::insert (const T elem)

Hope someone can help!

template <typename T, typename LESS_THAN>
void RestrictedSet::insert (const T elem) 
{
  _S.insert (elem);
  if (++_size > _max_size) drop_worst(); 
}

The problem must be somewhere in the template part, I get the error:

agenda.cpp:28: error: ‘template<class T> class RestrictedSet’ used without template parameters
agenda.cpp:28: error: variable or field ‘insert’ declared void
agenda.cpp:28: error: expected nested-name-specifier before ‘T’
agenda.cpp:28: error: expected ‘(’ before ‘T’

I also tried variants like:

void <T, LESS_THAN> RestrictedSet::insert (const T elem)

Hope someone can help!

When you define a template function outside the class body you need to put the template types after the class name too,

RestrictedSet<T,LESS_THAN>::insert

secondly your insert statements signature doesn't match in the class body and definition, one uses a reference.

As an aside, Usually it is better to place the template function definitions in the same file as the class declaration and not make a different .cpp for it since most compilers do not implement a separation model for template compilation, atleast i tried on g++ 4.4.1 and it was not working.

When you define a template function outside the class body you need to put the template types after the class name too,

RestrictedSet<T,LESS_THAN>::insert

Yeah, tried that before (as said in the post).

secondly your insert statements signature doesn't match in the class body and definition, one uses a reference.

Oops.

As an aside, Usually it is better to place the template function definitions in the same file as the class declaration and not make a different .cpp for it since most compilers do not implement a separation model for template compilation, atleast i tried on g++ 4.4.1 and it was not working.

A somewhat less obvious "feature" of g++, I have to say... Putting the code back into the header file gave the expected results indeed. Thanks!

Bart.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.