0

私の問題に対する答えは、以前に作成したスレッドの 1 つにあると確信しています。特に、"template" と "typename" キーワードをどこに、なぜ入れなければならないのでしょうか? テンプレート/タイプ名の明確化に関する優れた説明があります。ただし、相互に対話するクラステンプレートであるコードに概念を実際に拡張できないため、途方に暮れています。

このスレッドでは、コードで発生したのと同じエラーが表示されると思います。T が実際に必要な typename テンプレートであるのA<B>に対し、B がクラスである場所を使用して typedef を定義するのはなぜですか。A<T>

それにもかかわらず、私はこれらのオプションを試してみましたが、役に立ちませんでした。これがコードです。ご協力ありがとうございました。

#include "testTemplateA.h"
template<typename A>
class testTemplateB {
public:
    // none of these work
    typedef testTemplateA<A> templateType;
    typedef typename testTemplateA<A> templateType;
    typedef typename testTemplateA<testTemplateB> templateType;

    testTemplateB(templateType& TA) {}

    ~testTemplateB(void) {}
};

#include "testTemplateB.h"
template<typename A>
class testTemplateA
{
public:
    testTemplateA(void) {}

    ~testTemplateA(void) {}

    void callUponB(void) {
        testTemplateB<A> g = testTemplateB<A>(this);
    }



};
4

2 に答える 2

0

問題はこちら

typedef testTemplateA<A> templateType;

クラス テンプレートを使用してテンプレート クラスを作成しています

template<typename A>
class testTemplateA

テンプレート クラスを作成するときは、実際の型を指定する必要があります。したがって、このようになるはずです。

typedef testTemplateA<< testTemplateB<int >> templateType;

T が常にクラスであることが予想される場合は" class "を使用することをお勧めします。他の型 (int、char*、float など) が予想される場合は " typename " を使用することをお勧めします。使用上のヒントと考えてください。

于 2014-03-22T05:03:53.693 に答える
0

これは、テンプレートの構文の問題というよりは、循環依存の問題のように見えます。一方のクラスを他方の不完全なクラスで定義できる限り、次のようなことができます。

// Begin testTemplateA.h
#ifndef TEST_TEMPLATE_A_H
#define TEST_TEMPLATE_A_H

template<typename A>
class testTemplateA
{
public:
    testTemplateA(void) {}

    ~testTemplateA(void) {}

    void callUponB(void); // Can't be defined here!
};

#include "testTemplateB.h"

template<typename A>
void testTemplateA<A>::callUponB(void) {
    testTemplateB<A> g = testTemplateB<A>(this);
}

#endif
// End testTemplateA.h

// Begin testTemplateB.h
// Yes, the include is outside the define guard.
#include "testTemplateA.h"

#ifndef TEST_TEMPLATE_B_H
#define TEST_TEMPLATE_B_H

template<typename A>
class testTemplateB {
public:
    typedef testTemplateA<A> templateType;

    testTemplateB(templateType& TA) {}

    ~testTemplateB(void) {}
};

#endif
// End testTemplateB.h

ソース ファイルに testTemplateA.h だけが含まれている場合は、 のクラス テンプレート定義が表示されtestTemplateA、次に testTemplateB.h の内容全体が含まれ、次に依存する testTemplateA.h のメンバー定義が表示されtestTemplateBます。ソース ファイルに testTemplateB.h だけが含まれている場合は、すぐに testTemplateA.h で開始されます。これにはまだ途中に testTemplateB.h が含まれており、同じ結果が得られます。ソース ファイルにどちらかの順序で両方が含まれている場合、両方が既に含まれているため、2 番目のファイルは効果がありません。

typename少なくとも 1 つの::トークンを含む名前の前に、そのようなキーワードのみが必要です。

もう1つ、コンストラクターtestTemplateB(templateType& TA);は参照を期待していますが、ステートメントtestTemplateB<A> g = testTemplateB<A>(this);はポインター値を渡しますthis

于 2014-03-22T05:08:34.943 に答える