Interfacing and Inheritage of template classes in c++ [closed]

asked 2016-08-08 03:26:43 -0500

JanOr gravatar image

updated 2016-08-08 03:28:53 -0500

Hello everyone, 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<t>{ ^ /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
edit retag flag offensive reopen merge delete

Closed for the following reason duplicate question by JanOr
close date 2016-08-08 08:00:35.153235