1

I have a class, which have a public templated methods. This class has 2 strategies of behavior, which i want to pass via class template.

template<class Strategy>
class SomeClass {
public:
    template<class B>
    void ProcessType(){}
};

// And do something like this:
SomeClass<Strategy1> sc();
sc.ProcessType<SomeClassType>();
sc.ProcessType<SomeClassType2>();

SomeClass<Strategy2> sc2();
sc2.ProcessType<SomeClassType>();
sc2.ProcessType<SomeClassType2>();

But this code doesn't compile. I need to keep usage exact like this (to manipulate just via strategy).

Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121
dr11
  • 5,166
  • 11
  • 35
  • 77

1 Answers1

4

This is the problem:

SomeClass<Strategy1> sc();

That is a declaration of a function called sc that takes no arguments and returns a SomeClass<Strategy1>. This is commonly known as a vexing parse (but not the most vexing parse). What you want is:

SomeClass<Strategy1> sc;
Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
  • thanks, it compiles. But not build. i try to implement this method in .cpp file and it right me linkage error for this method (unresolved external ...). – dr11 Feb 22 '13 at 11:07
  • i do a such: http://stackoverflow.com/questions/886206/out-of-declaration-template-definitions-for-template-method-in-template-class – dr11 Feb 22 '13 at 11:08
  • @deeptowncitizen A member function of a class template must be defined in the header file. The compiler needs to be able to see the definition in all translation units. – Joseph Mansfield Feb 22 '13 at 11:55