Domanda

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).

È stato utile?

Soluzione

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;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top