C++ 错误:嵌套名称说明符中使用的类型不完整

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4572483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:48:21  来源:igfitidea点击:

Error: incomplete type used in nested name specifier

c++g++

提问by Ian

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

有 2 个非模板类AB具有一些静态模板方法。

From class Astatic method in Bis called and from class Bstatic method from Ais called. The source code only for illustration (not real code)...

从类A静态方法 inB被调用,从类B静态方法 fromA被调用。源代码仅用于说明(不是真正的代码)...

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

我在 NetBeans 中遇到了 g++ 编译器的问题。编译期间发生以下错误:错误:嵌套名称说明符 g++ 中使用的类型 A 不完整。

I tried to add forward declarations into both classes, but nothing was successful.

我试图在两个类中添加前向声明,但没有成功。

There is an older bug:

有一个较旧的错误:

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

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

回答by Adam Rosenfield

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
}

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 使用单独的头文件(这不会真正产生任何区别,因为它们最终会相互包含),您将需要重构它们,以便其中一个头文件不包含另一个头文件,因此至少需要在单独的文件中定义一个依赖模板函数。例如:

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

回答by Daniel Trebbien

Because there is a circular dependency, you need to carefully arrange the declarations of classes Aand Bso that they are both declared before the member functions are defined.

因为有一个循环依赖,你需要仔细安排的类的声明A,并B因此被定义的成员函数之前,它们都宣称。

Here is A.h:

这是A.h

#ifndef A_H
#define A_H 1
class A 
{
 public:
     template <class T>
     void f1 ();

     template <class T>
     T f2();
};

#include "B.h"

template <class T>
void A::f1()
{
     T var1= ...;
     T var2 = B::f4(T);
}

template <class T>
T A::f2()
{
     return ...
}

#endif

Here is B.h:

这是B.h

#ifndef B_H
#define B_H 1
class B
{
 public:
     template <class T>
     void f3 ();

     template <class T>
     T f4();
};

#include "A.h"

template <class T>
void B::f3()
{
     T var1= ...;
     T var2 = A::f2(T);
}

template <class T>
T B::f4()
{
     return ...
}

#endif

With this approach, you will be able to include either A.hor B.hfirst and not have a problem.

使用这种方法,您将能够包含A.hB.h首先包含其中之一,而不会出现问题。

回答by Gene Bushuyev

Your problem is circular header dependency.

您的问题是循环标题依赖性。