Reputation: 877
I want to overload conversion operator for two templates.
A.h
#pragma once
#include <B.h>
template <typename T> class A
{
operator B<T>() const;
}
B.h
#pragma once
#include <A.h>
template <typename T> class B
{
operator A<T>() const;
}
I got error
error C2833: 'operator A' is not a recognized operator or type see
reference to class template instantiation 'B<T>' being compiled
Although it works if conversion operator is overloaded only in one template.
Upvotes: 1
Views: 469
Reputation: 4039
You have a cyclic dependency problem. You need to have a forward declaration, such as:
A.h:
#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();
}
B.h:
#pragma once
#include "A.h"
template <class T>
class B {
operator A<T>() const {
bar();
}
};
I assume you used #include "A.h"
. A.h then included B.h. When the compiler started compiling B.h, it had not yet seen a declaration for A.h, and thus the compiler did not know how to interpret operator A<T>() const
, as it does not know that A is a type.
Upvotes: 2