简体   繁体   English

'ClassName' 不命名类型

[英]'ClassName' does not name a type

I have seen similar answers, but I don't seem to be able to solve mine just by looking at these for example ( this or that one for example).我看过类似的答案,但我似乎无法仅通过查看这些(例如这个那个)来解决我的问题。

So, I am having that .所以,我有那个。

Ah

#ifndef INCLUDE_CLASS_NAME
#define INCLUDE_CLASS_NAME

#include <B.h>

using namespace C;

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

#endif

In the include Bh I have this :在包含Bh我有这个:

namespace C{
namespace E{

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

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

Why am I getting this error in the mentioned line :为什么我在提到的行中收到此错误:

'D'  does not name a type

I am including the .h file, which defines the class.我包含了定义类的 .h 文件。 What am I missing?我错过了什么?

The symbol D is inside namespace E , which is inside namespace C , so the fully qualified name is C::E::D .符号D位于命名空间E ,而命名空间E位于命名空间C ,因此完全限定名称是C::E::D

So either:所以要么:

  • Add E:: to refer to D correctly:添加E::以正确引用D

     mutable E::D::DPtr myvariable;
  • Declare E in the using directive as well:using指令中也声明E

     using namespace C::E;

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

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

You're trying to call a function that does not exist yet according to the preprocessor.您正在尝试根据预处理器调用尚不存在的函数。 You need to define functions before you call them:您需要在调用它们之前定义函数:

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

This should get rid of the error.这应该摆脱错误。 This does not:这不会:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM