错误:嵌套名称说明符中使用的类型不完整
有 2 个非模板类 A
、B
具有一些静态模板方法。
从类 A
中调用 B
中的静态方法,从类 B
中调用 A
中的静态方法。源代码仅用于说明(不是真正的代码)...
啊,
#include "B.h"
class A
{
public:
template <class T>
void f1 ()
{
T var1= ...;
T var2 = B::f4(T);
}
template <class T>
T f2()
{
return ...
}
};
#include "A.h"
class B
{
public:
template <class T>
void f3 ()
{
T var1= ...;
T var2 = A::f2(T); //Error
}
template <class T>
T f4()
{
return ...
}
};
我在 NetBeans 中使用 g++ 编译器时遇到问题。在编译过程中,出现以下错误:错误:嵌套名称说明符 g++ 中使用了不完整的类型 A。
我尝试在这两个类中添加前向声明,但没有成功。
有一个较旧的错误:
There are 2 non-template classes A
, B
having some static template methods.
From class A
static method in B
is called and from class B
static method from A
is called. The source code only for illustration (not real code)...
A.h
#include "B.h"
class A
{
public:
template <class T>
void f1 ()
{
T var1= ...;
T var2 = B::f4(T);
}
template <class T>
T f2()
{
return ...
}
};
#include "A.h"
class B
{
public:
template <class T>
void f3 ()
{
T var1= ...;
T var2 = A::f2(T); //Error
}
template <class T>
T f4()
{
return ...
}
};
I am having troubles with g++ compiler in NetBeans. During the compilation the following error occurs: Error: incomplete type A used in nested name specifier, g++.
I tried to add forward declarations into both classes, but nothing was successful.
There is an older bug:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的头文件之间存在循环依赖关系。由于你的类是如此紧密地交织在一起,我建议将它们合并到一个头文件中,结构如下:
如果你坚持为 A 和 B 使用单独的头文件(这不会真正产生任何区别,因为它们最终包括彼此),您需要重新构造它们,以便其中一个标头不包含另一个标头,因此至少需要在单独的文件中定义一个依赖模板函数。例如:
You have a circular dependency between your header files. Since your classes are so tightly intertwined, I'd suggest merging them into a single header file, structured like this:
If you insist on using separate header files for A and B (which won't really make any difference since they end up including each other), you'll need to restructure them so that one of the headers doesn't include the other, so at least one of the dependent template functions will need to be defined in a separate file. For example:
由于存在循环依赖,因此您需要仔细安排类
A
和B
的声明,以便它们都在定义成员函数之前声明。这里是
Ah
:这里是
Bh
:通过这种方法,您将能够包含
Ah
或Bh
首先并没有问题。Because there is a circular dependency, you need to carefully arrange the declarations of classes
A
andB
so that they are both declared before the member functions are defined.Here is
A.h
:Here is
B.h
:With this approach, you will be able to include either
A.h
orB.h
first and not have a problem.您的问题是循环标头依赖性。
Your problem is circular header dependency.