0

だから私はこの問題を抱えています。

基本的に、テンプレート化されたインターフェースがあります:

template <typename T, typename U>
class                    Iinterface
{
 public:
 virtual ~Iinterface()
 // A pure methode for the example that gives me the problem
 virtual std::vector<T>        myMethod(T, U) = 0;
};

今のところ問題ありません。そこで、このインターフェースから継承するクラスを宣言します。

class                  firstclass : public Iinterface<std::string, int>
{
  // content doesnt matter. The problem is in second class inheritance.
  // here I just put the class so we can see how I inherited in 1st class.
};

これで、cpp ファイルに myMethod 宣言が追加されました。

template <>
std::vector<std::string>        firstClass::iInterface<std::string, int>::myMethod(std::string a, int b)
 {
      // methods code
 }

これまでのところ、問題はありません。myMethod 関数を宣言する 2 番目のクラスであり、型 T が戻り値と同じであるため、コンパイル エラーが発生します。

class                           secondClass : public IInterface<std::vector<std::string>, int>
{
  // class content
};

現時点ではコンパイルされますが、次のように myMethod を宣言すると:

template <>
std::vector<std::string>                     secondClass::Iinterface<std::vector<std::string> a, int n>
{
  // methodes code
}

ここで、std::vector の戻り値とメソッドの引数でエラーが発生します。テンプレートの競合だと思いますが、これを回避する方法が本当にわかりません。

最初のエラー:

28 :template-id ‘_out<>’ for ‘std::vector<std::basic_string<char> > Iio<std::vector<std::basic_string<char> >, int>::_out(std::vector<std::basic_string<char> >, int)’ does not match any template declaration

2 番目のエラー:

28 note: saw 1 ‘template<>’, need 2 for specializing a member function template

私はまだ C++ でコーディングする方法を学んでいて、すぐに習得できますが、時々行き詰まり、助けが必要になります。私がこれをやろうとしている方法は間違っていますか?この競合を回避するには、3 番目の型名を宣言する必要がありますか? (2つのタイプが同じタイプであるため、別の競合が発生すると考えていました)。

私がやろうとしていることは、これを行うための最良の方法ではないかもしれないことを知っていますが、私はまだ学んでいます.

詳細が必要な場合は、お気軽にお問い合わせください。

すべてのヘルプは非常に高く評価されます。ありがとう。

4

2 に答える 2

2

2 番目のクラス関数のオーバーロードの戻り値は次のようになります。

std::vector<std::vector<std::string> >

どのコンパイラを使用していますか、gcc はテンプレートの指定でエラーを出しました。

于 2013-01-31T17:21:07.603 に答える
2

原則として、テンプレート化されたコードを .h ファイルに保持するのが最善だと思います (コードはすべての翻訳単位で使用できる必要があるため、.cpp ファイルに配置する場合は、それらをコンパイルせずに含めます)。コードのタイプミス (iInterface と IInterface と Iinterface など) は別として、2 番目のクラスのメソッドは を返す必要がありstd::vector<std::vector<std::string> >ます。これは、戻り値の型がstd::vector<T>であり、 T が であるためですstd::vector<std::string>。以下のコード (interface.h を表す) は、clang++ v 3.3 を使用して mac os x で正常にコンパイルされます。

#include <string>
#include <vector>


template <typename T, typename U>
class Iinterface
{
public:
    virtual ~Iinterface();
    // A pure methode for the example that gives me the problem
    virtual std::vector<T> myMethod(T, U) = 0;
};

class firstclass : public Iinterface<std::string, int>
{
    // content doesnt matter. The problem is in second class inheritance.
    // here I just put the class so we can see how I inherited in 1st class.

    std::vector<std::string> myMethod(std::string a, int b)
    {
        // methods code
        // don't forget to return...
    }

};

class secondClass : public Iinterface<std::vector<std::string>, int>
{
    // class content
    std::vector<std::vector<std::string> > myMethod(std::vector<std::string> a, int n)
    {
        // methodes code
        // don't forget to return...
    }

};
于 2013-01-31T18:08:34.590 に答える