Hi,
Is it possible to have class template in non template class?
I'm trying something like this and I'm getting errors:

class Non_Template
{
//...other stuff
public:
template<class T>
class some_Template; //declaration
};
template<class T> //something WRONG with this syntax
Non_Template::some_Template
{
};

Err msg I'm getting:
error C2988: unrecognizable template declaration/definition
Thank you.

Recommended Answers

All 2 Replies

I think that you have got confused with the syntax of declaration and implementation. So here is an example with most of the common things you will likely need. [Please ask if I have missed something]

class Non_Template
{
  // stuff here
public:

  // Class DEFINITION
  template<typename T>
  class some_Template
    {
    public:
       
      T obj;
      
      void setObj(const T&);
   };

  // Two diferent uses of the template
  some_Template<int> IObj; 
  some_Template<double> DObj;
  
};

// Declartion of a member function outside of the class  
template<class T> 
void Non_Template::some_Template<T>::setObj(const T& A)
{
  obj=A;
  return;
}

int
main()
{
  Non_Template X;
  X.DObj.setObj(4.5);    // accessing the member via the class
}

Note I made the template declaration AND the objects public, that was not necessary so change as you wish.

StuXYZ - Thank you for your help.

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.