0

I have a problem with a project using template class inheritance. The idea is to have an agent that has a pointer to a msgtype. The msgtypes can differ, that's why a template class comes into game. The idea is to store the different message types via an interface class. To initialize the interface pointer in Agent with an instance of msgtype I need to include #include "msginterface.h" and #include "msgtype.h". Unfortunately, if I just include "msginterface.h", the project compiles just fine. But if I add #include "msgtype.h" in Agent.h that I require for the initialization. I get this crazy error:

The error I get 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 you have any idea what's the reason for this error?

The error can be reproduced with the 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
DentOpt
  • 77
  • 1
  • 6
  • 2
    The `MsgInterface` class is not a templated class, so why are you trying to use it as such? – Some programmer dude Aug 08 '16 at 13:02
  • 1
    Unrelated to your problem, but symbols (even preprocessor symbols like e.g. your `_AGENT` header guard macro) starting with an underscore followed by either another underscore or by an upper-case letter is reserved by the "implementation" (compiler and standard library) in all scopes. See [this question and its answers](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) for more nformation. – Some programmer dude Aug 08 '16 at 13:12

1 Answers1

0

You didn't declare MsgInterface as a templatized class.

Try something like:

template<class Agent>
class MsgInterface 
{
  Agent* agent;
}