有2个非模板类A
,B
有一些静态模板方法。
从类 A
调用 B
中的静态方法,并从类 B
调用 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 ...
}
};
我在使用 NetBeans 中的 g++ 编译器时遇到问题。在编译过程中出现以下错误:错误:在嵌套名称说明符g++中使用的类型A不完整。
我尝试将前向声明添加到两个类中,但没有成功。
有一个较旧的错误:
最佳答案
您的头文件之间存在循环依赖关系。由于您的类是如此紧密地交织在一起,我建议将它们合并到一个头文件中,结构如下:
class A
{
public:
template <class T>
void f1();
};
class B
{
...
};
template <class T>
void A::f1()
{
// Use full definition of class B
}
如果您坚持为 A 和 B 使用单独的头文件(这实际上并没有任何区别,因为它们最终会相互包含),您需要重新构造它们,以便其中一个头文件不包含另一个,因此至少需要在单独的文件中定义一个依赖模板函数。例如:
// File "a_no_b.h"
class A
{
public:
template <typename T>
void f1();
};
// File "b_no_a.h"
class B
{
public:
template <typename T>
void f3();
};
// File "a.h"
#include "a_no_b.h"
#include "b_no_a.h"
template <typename T>
void A::f1()
{
// Use full definition of class B
}
// File "b.h"
#include "b_no_a.h"
#include "a_no_b.h"
template <typename T>
void B::f3()
{
// Use full definition of class A
}
关于c++ - 错误 : incomplete type used in nested name specifier,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4572483/