错误:在嵌套名称说明符中使用不完整的类型 [英] Error: incomplete type used in nested name specifier

查看:781
本文介绍了错误:在嵌套名称说明符中使用不完整的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两个非模板类 A B 有一些静态模板方法。

There are 2 non-template classes A, B having some static template methods.

类中的静态方法调用 B c $ c> B 静态方法从 A 被调用。源代码仅供参考(非实际代码)...

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)...

Ah

#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 ++编译器的问题。在编译期间发生以下错误:错误:不完整类型A用于嵌套名称说明符,g ++。

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.

有一个较旧的错误:

http://gcc.gnu.org/ml/gcc-bugs/2005-02/msg01383.html p>

http://gcc.gnu.org/ml/gcc-bugs/2005-02/msg01383.html

推荐答案

您的头文件之间有循环依赖关系。因为你的类是如此紧密的交织在一起,我建议将它们合并到一个单一的头文件,结构如下:

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:

class A
{
public:
  template <class T>
  void f1();
};

class B
{
  ...
};

template <class T>
void A::f1()
{
  // Use full definition of class B
}

如果你坚持使用A和B的单独的头文件(这将不会产生任何差异,因为他们最终包括在一起),你需要重组它们一个头部不包括另一个,因此至少一个依赖模板函数将需要在单独的文件中定义。例如:

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:

// 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
}

这篇关于错误:在嵌套名称说明符中使用不完整的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆