不在 C++ 中命名类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1761018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does Not Name A Type in C++
提问by JT.
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
。
Example, I use:
例如,我使用:
typedef CP_M_ReferenceCounted FxRC;
and I have included CP_M_ReferenceCounted.h
in yyy.h
我已经包括CP_M_ReferenceCounted.h
在yyy.h
I am missing some basic understanding, what is it?
我缺少一些基本的理解,它是什么?
回答by Test
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;" fixes the problem as below:
但是添加一行“使用命名空间 Yyy;” 修复问题如下:
//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 标头中的命名空间范围。
回答by John Weldon
The inclusion of the CP_M_ReferenceCounted type is probably lexically AFTER the typedef... can you link to the two files directly, or reproduce the problem in a simple sample?
CP_M_ReferenceCounted 类型的包含可能是在 typedef 之后的词法上的……您可以直接链接到这两个文件,或者在一个简单的示例中重现该问题吗?
回答by Owl
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
回答by wangzheqie
Yes, you should try to check the
namespace
first.
是的,您应该尝试检查第
namespace
一个。
回答by skittlebiz
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. 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 文件,并在新的 #ifndef 中保持与原始文件中相同的“YYY_H__”。我只是这样做并意识到我的错误。确保更新您的新 YYY_H__ 以表示新类。否则,很明显, YYY_H__ 将已经从原始文件中定义,并且将跳过此标头中的重要内容。