简体   繁体   English

没有在 C++ 中命名类型

[英]Does Not Name A Type in C++

in C++ when i get an error that says xxxxx does not name a type in yyy.h在 C++ 中,当我收到一条错误消息时说 xxxxx 没有在 yyy.h 中命名类型

What does that mean?那是什么意思?

yyy.h has included the header that xxxx is in. yyy.h包含了xxxx所在的 header。

Example, I use:例如,我使用:

typedef CP_M_ReferenceCounted FxRC;

and I have included CP_M_ReferenceCounted.h in yyy.h我在yyy.h中包含CP_M_ReferenceCounted.h

I am missing some basic understanding, what is it?我缺少一些基本的理解,这是什么?

That seems you need to refer to the namespace accordingly. 这似乎需要相应地引用命名空间。 For example, the following yyy.h and test.cpp have the same problem as yours: 例如,以下yyy.h和test.cpp与您的问题相同:

//yyy.h
#ifndef YYY_H__
#define YYY_H__

namespace Yyy {

class CP_M_ReferenceCounted
{
};

}

#endif

//test.cpp
#include "yyy.h"

typedef CP_M_ReferenceCounted FxRC;


int main(int argc, char **argv)
{

        return 0;
}

The error would be 错误将是

...error: CP_M_ReferenceCounted does not name a type

But add a line "using namespace Yyy;" 但添加一行“使用命名空间Yyy;” fixes the problem as below: 修复问题如下:

//test.cpp
#include "yyy.h"
// add this line
using namespace Yyy;

typedef CP_M_ReferenceCounted FxRC;
...

So please check the namespace scope in your .h headers. 因此,请检查.h标头中的命名空间范围。

包含CP_M_ReferenceCounted类型可能是词法上的,因为typedef ...你可以直接链接到这两个文件,还是在一个简单的样本中重现问题?

Although possibly unrelated to OP's original question... this is an error I just had and shows how this error could occur. 虽然可能与OP的原始问题无关......这是我刚才遇到的一个错误,并说明了这个错误是如何发生的。

When you define a type in a C++ class and you return it, you need to specify the class in which the type belongs. 在C ++类中定义类型并返回它时,需要指定类型所属的类。

For example: 例如:

class ClassName{
public:
 typedef vector<int> TypeName;
 TypeName GetData();
};

Then GetName() must be defined as: 然后GetName()必须定义为:

ClassName::TypeName ClassName::GetData(){...}

not

TypeName ClassName::GetData(){...}

Otherwise the compiler will come back with the error: 否则编译器将返回错误:

error: 'TypeName' does not name a type

Be sure you didn't copy paste this yyy.h file from some other class header and keep the same "YYY_H__" in the new #ifndef as in the original file.请确保您没有从其他 class header 复制粘贴此 yyy.h 文件,并在新的 #ifndef 中保留与原始文件中相同的“YYY_H__”。 I just did this and realized my mistake.我只是这样做并意识到我的错误。 Make sure to update your new YYY_H__ to represent the new class. Otherwise, obviously, YYY_H__ will already be defined from the original file and the important stuff in this header will be skipped.确保更新您的新 YYY_H__ 以代表新的 class。否则,显然,YYY_H__ 已经从原始文件中定义,并且将跳过此 header 中的重要内容。

是的,您应该首先尝试检查namespace

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

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