1

Executor クラスには P 型のテンプレートがあり、コンストラクターで P オブジェクトを受け取ります。Algo クラスにはテンプレート E があり、E 型の静的変数もあります。Processor クラスには、テンプレート T と T のコレクションがあります。

Executor< Processor<Algo> >質問とをどのように定義できますAlgo<Executor>か? これは可能ですか?これを定義する方法がわかりません。「無限再帰テンプレート引数」のようなものです。

コードを参照してください。

template <class T>
class Processor { 
    map<string,T> ts;
    void Process(string str, int i)
    {
        ts[str].Do(i);
    }
} 

template <class P>
class Executor {
    P &p;
    Executor(P &inp) : p(inp) {}

    void Bar(string str, int i) {
        p.Process(str,i);
    }

    Execute(string str)
    {
    }
} 

template <class E>
class Algo
{
    static E e;

    void Do(int i) {}
    void Foo()
    {
        e.Execute("xxx");
    }
}

main ()
{
    typedef Processor<Algo> PALGO; // invalid
    typedef Executor<PALGO> EPALGO;
    typedef Algo<EPALGO> AEPALGO;

    Executor<PALGO> executor(PALGO());
    AEPALGO::E = executor;
}

編集* ** * ** * ** * ** * ** * ** * ** * ** * ***

少し明確にします。Executor は、サービスを提供するシングルトンです。すべての Algo オブジェクトには Executor のサービスが必要です。Executor は、特定の Algo オブジェクトに送信する必要があるレポートを生成することがあります。これらは、プロセッサを介して正しいアルゴリズムに送信されます。

基本的な問題は、Executor を定義するには Algo が必要であり、Algo を定義するには Executor が必要であるということです。

4

5 に答える 5

2

コードを再現しようとしましたが、何を達成しようとしているのかよくわかりません。まず、これを次のように変更しました。

   #include <string>   //Added
   #include <map>      //Added
   using namespace std;//Added

   template <class T>
   class Processor {    
      map<string,T> ts;    
      void Process(string str, int i) {        
         ts[str].Do(i);    
      }
   };

   template <class P>
   class Executor {
      Processor<P> &p;    //Was Proc ???
      Executor(P &p) : Processor<P>(p) {}    //Was Proc ???
      void Foo(string str, int i) {
         p.Process(str,i);
      }
      void Execute(string str){}  //Added return type void
   };

   template <class E>
   class Algo {
   public:                 //Added
      static E e;
      void Do(int i) {}
   };

   main () {
      typedef Processor< Algo<int> > PALGO; //Added template argument to Algo
      typedef Executor<PALGO> EPALGO;
      typedef Algo<EPALGO> AEPALGO;

      Executor<PALGO> executor(PALGO());
      AEPALGO::e = executor;
   }

Executor 定義で Proc を Processor に変更 - (Proc とは何ですか?) typedef Processor> PALGO; でテンプレート引数を指定しました。次に、AEPAGO::E --> これはテンプレート パラメータであり、クラス Algo メンバーではありません。つまり、AEPAGO::e です。これで、より管理しやすいエラーが表示されます。型を変換するには、コピー コンストラクターが必要です。

于 2012-12-19T23:26:28.870 に答える
1

AFAICS、同じ Executorタイプでそれを行うことはできません。それ以外の場合は、定義する必要があります

Executor<Processor<Algo<Executor<Processor<Algo<...> > > > > >

技術的に意味がある場合は、他のタイプで定義すると機能する可能性があります

class X {
...
};

Executor<Processor<Algo<Executor<Processor<Algo<X> > > > > >

またはtypedef

class X {...};
typedef Processor<Algo<X> > PALGO;
typedef Executor<PALGO> EPALGO;
typedef Algo<EPALGO> AEPALGO;

Executor<PALGO> executor(PALGO());
于 2012-12-19T22:36:00.430 に答える
1

継承を使用できます。

class X : public Executor<Processor<Algo<X>>> {};

そうでなければ、これは不可能です。

于 2012-12-19T22:40:16.623 に答える
0

Executorはシングルトンであるため、独自のシングルトンクラスまたはExecutorのいずれかでAlgoからその定義を移動できます。次に、Executorテンプレートメンバー関数について知る必要があるすべてのAlgo関数を作成します。

template <class P>
class Executor {

    static Executor e;

    P &p;
    Executor(P &p) : Proc(p) {}

    void Bar(string str, int i) {
        p.Process(str,i);
    }

    Execute(string str)
    {
    }

    public:
    static Executor& getE(){ return e;}
} 

class Algo
{

    void Do(int i) {}
    template <class E>
    void Foo()
    {
        E::getE().Execute("xxx");
    }
}
于 2012-12-20T01:34:14.320 に答える
0

解決しました!コメントを参照してください。// * ***

#include <string>
#include <map>
#include <iostream>
using namespace std;

template <class T>
class Processor {
public:
    map<string,T*> ts;
    void Process(string str, int i)
    {
        ts[str]->Do(i);
    }
};

template <class P>
class Executor  {
public:
    P &p;
    Executor(P &inp) : p(inp) {}

    void Bar(string str, int i) {
        p.Process(str,i);
    }

    void Execute(string str)
    {
        cout << " Executor::Execute " << str << endl;
    }
};

template <template <class> class E> //**********
class Algo
{
    string str;
public:
    Algo(const string &s) : str(s) {}

    static E<Processor<Algo>> *e; //**********

    void Do(int i) { cout << str << "::Do(" << i <<")"<< endl; }

    void Foo()
    {
        e->Execute(str);
    }
};

template <template <class> class E>
E< Processor<Algo<E> > >* Algo<E>::e;  //**********

int main(int argc, char **argv)
{
    typedef Algo<Executor> EALGO;
    typedef Processor<EALGO> PALGO;
    typedef Executor<PALGO> EPALGO;

    PALGO p;
    EPALGO executor(p);

    EALGO::e = &executor; //**********

    EALGO ealgo1("algo1"), ealgo2("algo2");

    p.ts["algo1"] = &ealgo1;
    p.ts["algo2"] = &ealgo2;
    ealgo1.Foo();
    ealgo2.Foo();
    executor.Bar("algo1",1111);
    executor.Bar("algo2",2222);

}
于 2012-12-20T02:44:26.923 に答える