class - Interfacing and Inheritage of template classes in c++ -


i have problem project using template class inheritance. idea have agent has pointer msgtype. msgtypes can differ, that's why template class comes game. idea store different message types via interface class. initialize interface pointer in agent instance of msgtype need include #include "msginterface.h" , #include "msgtype.h". unfortunately, if include "msginterface.h", project compiles fine. if add #include "msgtype.h" in agent.h require initialization. crazy error:

the error is:

error: expected template-name before ‘<’ token class msg:public msginterface{ ^ /home/catkin/src/template_class/src/msg.h:10:30: error: expected ‘{’ before ‘<’ token /home/catkin/src/template_class/src/msg.h:10:30: error: expected unqualified-id before ‘<’ token

do have idea what's reason error?

the error can reproduced following code:

//main.cpp

#include <stdio.h> #include <iostream> #include <agent.h> using namespace std;  int main(void){ cout<<"hello world"<<endl; } 

//agent.h

#ifndef _agent #define _agent #include "msginterface.h" #include "msgtype.h"  class agent{ msginterface* msg; }; #endif 

//msginterface.h

#ifndef _msginterface #define _msginterface  #include <stdio.h> #include <agent.h> using namespace std;  class agent; //forward declaration class msginterface{ agent* agent; }; #endif 

//msg.h

#ifndef _msg #define _msg  #include <stdio.h> #include <agent.h> #include "msginterface.h" using namespace std;  template <class t> class msg:public msginterface<t>{ }; #endif 

//msgtype.h

#ifndef _msgtype #define _msgtype #include <stdio.h> #include "agent.h" #include "msg.h" using namespace std;  template <class s> class msgtape:public msg<s>{ }; #endif 

you didn't declare msginterface templatized class.

try like:

template<class agent> class msginterface  {   agent* agent; } 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -