templates - C++ templated class and generics are not working -


i have this:

template<class e, class p> class listenerinterface { public:     virtual listenerresponse handleevent(e<p> *event) const = 0; }; 

but e , p both templated classes default template parameters.

and i'm getting this:

error: expected ')' virtual listenerresponse handleevent(e<p> *event) const = 0; 

using cmake -std=c++14 -wall -w -pedantic

here "minimal, complete, , verifiable" example demonstrates not how resolve immediate error, how create listener class implements listenerinterface, while satisfying condition that:

e , p both templated classes default template parameters.

first, define classes correspond p , e in example:

template <typename t = int> class payload { public:   payload (const t & value) : value(value) {}   const t value; };  template <typename t = payload<>> class event { public:   event (const t & payload) : payload(payload) {}   const t payload; }; 

next, define listenerresponse , listenerinterface:

class listenerresponse { public:   listenerresponse (const std::string & message) : message(message) {}   const std::string message; };  template <typename t> class listenerinterface { public:   virtual listenerresponse handleevent (t * event) const = 0; }; 

note template<class e, class p> has been replaced template <typename t>.

now, define listener class implements listenerinterface:

template <typename t = event<>> class listener : public listenerinterface<t> { public:   listenerresponse handleevent (t * event) const {     std::stringstream stream;     stream << "event payload value: " << event->payload.value;     return listenerresponse(stream.str());   } }; 

then define main function puts these pieces together:

int main () {   payload<> payload(123);   event<> event(payload);   listener<> listener;   listenerresponse response = listener.handleevent(&event);   std::cout << response.message << std::endl; } 

here full program, including requisite #include statements:

#include <iostream> #include <sstream> #include <string>  template <typename t = int> class payload { public:   payload (const t & value) : value(value) {}   const t value; };  template <typename t = payload<>> class event { public:   event (const t & payload) : payload(payload) {}   const t payload; };  class listenerresponse { public:   listenerresponse (const std::string & message) : message(message) {}   const std::string message; };  template <typename t> class listenerinterface { public:   virtual listenerresponse handleevent (t * event) const = 0; };  template <typename t = event<>> class listener : public listenerinterface<t> { public:   listenerresponse handleevent (t * event) const {     std::stringstream stream;     stream << "event payload value: " << event->payload.value;     return listenerresponse(stream.str());   } };  int main () {   payload<> payload(123);   event<> event(payload);   listener<> listener;   listenerresponse response = listener.handleevent(&event);   std::cout << response.message << std::endl; } 

save program example.cc, compile compiler flags specified, , run verify produces expected output:

$ clang++ -std=c++14 -wall -w -pedantic example.cc -o example $ ./example event payload value: 123 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -