【问题标题】:'ClassName' does not name a type'ClassName' 没有命名类型
【发布时间】:2015-06-12 08:39:48
【问题描述】:

我已经看到了类似的答案,但我似乎无法仅通过查看这些答案来解决我的问题(例如 thisthat 之一)。

所以,我有那个。

啊.h

#ifndef INCLUDE_CLASS_NAME
#define INCLUDE_CLASS_NAME

#include <B.h>

using namespace C;

D::DPtr myvariable;  <-- Error in here

#endif

在包含 B.h 我有这个:

namespace C{
namespace E{

class D
{
  public:
      typedef shared_ptr<D> DPtr;
}

} //end of namespace E
} // end of namespace C

为什么我在上述行中收到此错误:

'D'  does not name a type

我包括定义类的 .h 文件。我错过了什么?

【问题讨论】:

  • 为什么将myvariable 声明为mutable?此概念仅适用于班级成员。
  • @Mikhail 是的,改变了这一点,我之前无法编译。

标签: c++


【解决方案1】:

符号D 在命名空间E 内,而在命名空间C 内,因此完全限定名称为C::E::D

所以要么:

  • 添加E::以正确引用D

    mutable E::D::DPtr myvariable;
    
  • using 指令中声明E

    using namespace C::E;
    

【讨论】:

    【解决方案2】:

    你错过了命名空间 E...

    mutable E::D::DPtr myvariable; // should work
    

    【讨论】:

      【解决方案3】:

      您正在尝试根据预处理器调用一个尚不存在的函数。您需要在调用它们之前定义函数:

      int mainFunction(){
          int foo(int bar){ //define function FIRST...
              return bar;
          }
      foo(42) //call it later.
      }
      

      这应该可以消除错误。 这不是:

      int mainFunction(){
          foo(42) //trying to call a function before it's declared does not work.
              int foo(int bar){
              return bar;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2022-01-21
        • 2023-04-07
        • 2021-02-03
        • 2017-04-01
        • 2015-06-09
        • 2016-04-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多