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

发布于 2024-10-09 21:19:55 字数 905 浏览 0 评论 0原文

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

从类 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。

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

有一个较旧的错误:

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

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:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤城病女 2024-10-16 21:19:55

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

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
}

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:

// 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
}
烏雲後面有陽光 2024-10-16 21:19:55

由于存在循环依赖,因此您需要仔细安排类 AB 的声明,以便它们都在定义成员函数之前声明。

这里是 Ah

#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

这里是 Bh

#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

通过这种方法,您将能够包含 AhBh首先并没有问题。

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

Here is 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:

#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.h or B.h first and not have a problem.

你好,陌生人 2024-10-16 21:19:55

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

Your problem is circular header dependency.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文