1

2つのテンプレートの変換演算子をオーバーロードしたい。

ああ

#pragma once

#include <B.h>

template <typename T> class A
{
operator B<T>() const;
}

Bh

#pragma once

#include <A.h>

template <typename T> class B
{
operator A<T>() const;
}

エラーが発生しました

error C2833: 'operator A' is not a recognized operator or type see
reference to class template instantiation 'B<T>' being compiled

ただし、変換演算子が1つのテンプレートでのみオーバーロードされている場合は機能します。

4

1 に答える 1

2

循環依存の問題があります。次のような前方宣言が必要です。

ああ:

#pragma once

template <class T> class B;

template <class T> class A {
   operator B<T>() const;
};

#include "B.h"

template <class T>
A<T>::operator B<T>() const {
   foo();
}

Bh:

#pragma once
#include "A.h"

template <class T>
class B {
   operator A<T>() const {
      bar();
   }
};

使用したと思います#include "A.h"。その後、AhにBhが含まれましたコンパイラがBhのコンパイルを開始したとき、Ahの宣言はまだ表示さoperator A<T>() constれていなかったため、Aが型であることを認識していないため、コンパイラは解釈方法を知りませんでした。

于 2013-03-04T18:17:33.977 に答える