'ClassName'不命名类型

'ClassName' does not name a type

本文关键字:类型 ClassName      更新时间:2023-10-16

我见过类似的答案,但我似乎无法仅通过查看这些(例如这个或那个)来解决我的答案。

所以,我有那个.

#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 文件。我错过了什么?

符号D在命名空间E内,在命名空间C内,所以完全限定名是C::E::D

因此,请执行以下任一操作:

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

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

    using namespace C::E;
    

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

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

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

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