Search code examples
c++templatesvisual-c++friend

non-class template has already been declared as a class template


I have cloned a project from GitHub which is implemented targeting Linux (using Linux specific socket) to use in windows with VC++.

Have modified the needed part to match windows but compiling the singleton class I get the error which I have no clue about and searching similar question did not give me any hint.

error C2990: 'ISingleton': non-class template has already been declared as a class template

Singleton.h
------------
#define SINGLETON_ACCESS friend class ISingleton;
template<class T>
class ISingleton {
protected:
    ISingleton() {}
    static T* mInstance;
public:  virtual ~ISingleton(){}
} /* class ISingleton */
template<class T>
T* ISingleton<T>::mInstance = NULL;

and

factory.h
-----------
namespace J1939 {
   class J1939Frame;
   class J1939Factory : public ISingleton<J1939Factory> {
     SINGLETON_ACCESS; /* <---Getting Error Here */
     virtual ~J1939Factory();
   private:
     J1939Factory();
/* ..... */
}

Solution

  • The problem is that you define friend the class ISingleton

    friend class ISingleton;
    

    where ISingleton is a template class.

    template<class T>
    class ISingleton { /* ... */ };
    

    You can't: defining it friend you have to specify a template type for it; by example (is what do you want?)

    friend class ISingleton<J1939Factory>;