8

暗黙のパラメーターを持つテンプレート クラスの宣言があります。

List.h

template <typename Item, const bool attribute = true>
class List: public OList <item, attribute>
{
    public:
    List() : OList<Item, attribute> () {}
    ....
};

別のヘッダー ファイルでフロー フォワード宣言を使用しようとしました。

分析.h

template <typename T, const bool attribute = true>
class List;

しかし、G++ には次のエラーが表示されます。

List.h:28: error: redefinition of default argument for `bool attribute'
Analysis.h:43: error:   original definition appeared here

暗黙的なパラメーターなしで前方宣言を使用する場合

template <typename T, const bool attribute>
class List;

コンパイラはこの構造を受け入れません

分析.h

void function (List <Object> *list)
{
}

次のエラーが表示されます (つまり、暗黙的な値を受け入れません)。

Analysis.h:55: error: wrong number of template arguments (1, should be 2)
Analysis.h:44: error: provided for `template<class T, bool destructable> struct List'
Analysis.h:55: error: ISO C++ forbids declaration of `list' with no type

更新された質問:

テンプレート定義からデフォルト パラメータを削除しました。

List.h

template <typename Item, const bool attribute>
class List: public OList <item, attribute>
{
    public:
    List() : OList<Item, attribute> () {}
    ....
};

クラス List を使用する最初のファイルには、パラメーター属性の暗黙的な値を持つ前方宣言があります

Analysis1.h

template <typename T, const bool attribute = true>
class List;  //OK

class Analysis1
{
    void function(List <Object> *list); //OK
};

クラス リストを使用する 2 番目のクラスは、暗黙の値を使用して前方定義を使用します。

Analysis2.h

template <typename T, const bool attribute = true> // Redefinition of default argument for `bool attribute'
class List; 

class Analysis2
{
    void function(List <Object> *list); //OK
};

クラス List を使用する 2 番目のクラスは、暗黙の値を使用した前方定義なし

Analysis2.h

template <typename T, const bool attribute> // OK
class List; 

class Analysis2
{
    void function(List <Object> *list); //Wrong number of template arguments (1, should be 2)
};
4

5 に答える 5

5

単純。前方宣言で既に言及しているため、定義からデフォルト値を削除します。

template <typename Item, const bool attribute = true> //<--- remove this 'true`
class List: public OList <item, attribute>
{
  //..
};

書く:

template <typename Item, const bool attribute>  //<--- this is correct!
class List: public OList <item, attribute>
{
  //..
};

オンラインデモ : http://www.ideone.com/oj0jK

于 2011-02-05T09:43:45.587 に答える
2

考えられる解決策は、別のヘッダー ファイル List_fwd.h を宣言することです。

template <typename Item, const bool attribute>
class List;

したがって、List.h と Analysis.h の両方で、先頭に List_fwd.h を含めます。したがって、 List.h は次のようになります

#include "List_fwd.h"

template <typename Item, const bool attribute = true>
class List: public OList <item, attribute>
{
    public:
    List() : OList<Item, attribute> () {}
    ...
};

そしてAnalysis.h

#include "List_fwd.h"
于 2011-02-05T10:45:48.010 に答える
1

最初の宣言のみがパラメーターのデフォルト値を持つようにする必要があります。これは、最初に forward-declaration-only ヘッダーを定義し、次に と の両方から含めることで実現できList.hますAnalysis.h。の定義にList.hは、デフォルト値を含めないでください。

于 2011-02-05T09:43:54.347 に答える
0

デフォルト パラメータは 1 か所でのみ定義できます (特定の翻訳に対して)。クラス宣言でこれを行うのが最も便利ですが、前方で定義するのは悪い考えです。

forward にはデフォルトのパラメーターは必要ありません (場合によっては入力する必要があります)。

デフォルトのパラメーターが本当に必要な場合は、転送と組み合わせてこれを実装する別の単純なテンプレート タイプを作成できます。次に、typedef を介して結果にアクセスします。リストの転送を使用してこれを行うことができます。

于 2011-02-05T09:43:43.437 に答える
0

List.h 使用しているすべてのファイルに含める必要があります。宣言は非テンプレート型に対してのみ十分です。テンプレート タイプの場合、すべてのコンパイル ユニットのヘッダー ファイルをインクルードする必要があります。

于 2011-02-05T10:05:43.790 に答える