0

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();
/* ..... */
}
Amir-Mousavi
  • 4,273
  • 12
  • 70
  • 123

1 Answers1

2

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>;
max66
  • 65,235
  • 10
  • 71
  • 111
  • Thanks, for explaining, so if other classes also want to use the `ISingleton` how it should be defined. I mean I am new to C++ and don't know why after creating a class with extending `ISingleton` calling the MACRO will not work – Amir-Mousavi Feb 18 '19 at 20:32
  • @Amir-Mousavi - not sure to understand your problem; take in count that `ISingleton` isn't a class but a collection of classes; you can declare `friend` a class, not the full collection; so you have to specify the required template parameters (only a type in this case). I don't understand why you want `ISingleton` `friend`. – max66 Feb 18 '19 at 20:37
  • well, I should accept it as the answer instead of calling the Macro, `friend class ISingleton;` – Amir-Mousavi Feb 18 '19 at 21:34