'的ClassName'没有命名类型

时间:2015-06-12 08:39:49

标签: c++

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

所以,我有这个。

A.H

for (index, element) in list.enumerate() {
    print("Item \(index): \(element)")
}

在包含 B.h 中,我有:

#ifndef INCLUDE_CLASS_NAME
#define INCLUDE_CLASS_NAME

#include <B.h>

using namespace C;

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

#endif

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

namespace C{
namespace E{

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

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

我包含.h文件,它定义了类。我错过了什么?

3 个答案:

答案 0 :(得分:5)

符号D位于名称空间E内,位于名称空间C内,因此完全限定名称为C::E::D

所以:

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

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

    using namespace C::E;
    

答案 1 :(得分:4)

您错过了名称空间E ...

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

答案 2 :(得分:0)

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

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;
    }
}
相关问题