【发布时间】:2017-01-18 07:53:52
【问题描述】:
我有以下代码在 Visual Studio 和 g++ 下编译良好,但在 Clang 中我收到错误“错误:'ns::B' 是不完整的类型”
啊.h
#pragma once
namespace ns
{
class B;
class A
{
friend class B;
class Inner
{
public:
int x;
Inner(int x) : x(x) {}
};
public:
template<typename T>
T getB(int i)
{
B b = B(Inner(i));
return T(b);
}
};
}
B.h
#pragma once
#include "A.h"
namespace ns
{
class B
{
A::Inner i;
public:
B(A::Inner i) : i(i)
{}
operator int() const
{
return i.x;
}
};
}
main.cpp
#include "A.h"
#include "B.h"
int main()
{
ns::A a;
return a.getB<int>(5);
}
据我了解,代码应该可以工作,因为当模板被实例化时,B 已经完成。它是否正确?如果是这样,有没有办法在 Clang 中解决这个问题?
【问题讨论】:
-
不相关的旁注:main.cpp 中的
#include "a.h"没有意义。 -
@SingerOfTheFall 重复包含标题很好,但它们必须包含此处未显示的保护。
-
@Potatoswatter 是的,对不起,我在复制时确实错过了。