2016-08-08 7 views
0

テンプレートクラスの継承を使用するプロジェクトに問題があります。 考え方は、msgtypeへのポインタを持つエージェントを持つことです。 msgtypesが異なる可能性があります。そのため、テンプレートクラスがゲームになるのです。 アイデアは、インターフェイスクラスを介して異なるメッセージタイプを格納することです。 エージェント内のインタフェースポインタをmsgtypeのインスタンスで初期化するには、 に#include "msginterface.h"と#include "msgtype.h"を含める必要があります。 残念ながら、 "msginterface.h"だけをインクルードすると、プロジェクトはうまくコンパイルされます。 しかし、#include "msgtype.h"をエージェントの初期化に必要なAgent.hに追加してください。私は、このクレイジーなエラーが発生します。C++のテンプレートクラスのインターフェイスと継承

私が手にエラーがある:

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

あなたは、このエラーの原因何任意のアイデアを持っていますか?

//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 
+2

'MsgInterface'クラスはテンプレートクラスではないので、なぜあなたはそのように使用しようとしていますか? –

+1

あなたの問題とは無関係ですが、アンダースコアで始まり、アンダースコアまたは大文字で始まるシンボル(例: '_AGENT'ヘッダガードマクロのようなプリプロセッサシンボルさえも)は、"実装 "(コンパイラと標準ライブラリすべてのスコープで詳細については、[この質問とその回答](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier)を参照してください。 –

答えて

0

あなたはテンプレート化されたクラスとしてMsgInterfaceを宣言しませんでした。

のようなものを試してみてください:

template<class Agent> 
class MsgInterface 
{ 
    Agent* agent; 
} 
関連する問題