C++ Qt Creator:“XYZ 没有命名类型”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1421658/
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
Qt Creator: “XYZ does not name a type”
提问by Tony the Pony
This is a very frustrating error message in Qt Creator: 'XYZ' does not name a type
. This usually means that there is an error in the class XYZ
that prevents the compiler from generating the type, but there are no additional hints as to what went wrong.
这是Qt Creator中一个非常令人沮丧的错误信息: 'XYZ' does not name a type
。这通常意味着类中XYZ
存在阻止编译器生成类型的错误,但没有其他提示说明出了什么问题。
Any suggestions?
有什么建议?
采纳答案by changfeng
I found this problem on qtcreator 3.4.1 and QT 5.4, when I replace such as
我在 qtcreator 3.4.1 和 QT 5.4 上发现了这个问题,当我替换诸如
#include <QTextEdit>
with
和
class QTextEdit;
this problem gone.
这个问题没了。
回答by nabil
i just had this problem, and like Arckarophhave said : the problem is that when we include a header file in a source code file, and we use in it the directive #ifndef, we can't include it again in a header file to give a type of it's included class to a variable in source code file
我刚刚遇到了这个问题,就像Arckaroph所说:问题是当我们在源代码文件中包含一个头文件,并在其中使用指令 #ifndef 时,我们不能将它再次包含在头文件中将其包含的类的类型赋予源代码文件中的变量
example :
例子 :
class1.h contains Class1 class2.h contains Class2 class2 have a private variable V with class1 type if we include class1.h in class2.CPPwe can't include it in class2.h to give V a class1 type.
class1.h 包含 Class1 class2.h 包含 Class2 class2 如果我们将 class1.h 包含在 class2 中,则具有 class1 类型的私有变量 V。CPP我们不能把它包含在 class2.h 中来给 V 一个 class1 类型。
so we put in class2.cpp class2.h before class1.h or we delete class1.h from class2.cpp
所以我们在 class1.h 之前放入 class2.cpp class2.h 或者我们从 class2.cpp 中删除 class1.h
回答by Steve
In your abc.cpp
make sure you include xyz.h
before including abc.h
.
在您abc.cpp
确保包含xyz.h
之前包含abc.h
.
No idea why swapping the two around would make a difference however it did for me.
不知道为什么交换两者会有所不同,但这对我来说确实如此。
回答by rpg
Do you get the error from the compiler or the IDE (as a squiggly underline)? I've encountered this in Qt Creator 1.2.9 and I think it's a bug in the IDE.
您是否从编译器或 IDE(作为波浪形下划线)收到错误消息?我在 Qt Creator 1.2.9 中遇到过这个问题,我认为这是 IDE 中的一个错误。
回答by Arckaroph
I believe you are declaring something of type XYZ such as
我相信您正在声明 XYZ 类型的内容,例如
XYZ foo;
The problem is XYZ is not yet defined.
问题是 XYZ 尚未定义。
The following is my problem and my conclusion. What do you think?
以下是我的问题和我的结论。你怎么认为?
My problem is I have a class ABC and a class XYZ. Class ABC has a member that is declared as a XYZ type. Class XYZ has a member that is declared as a ABC type. The compiler doesn't know what the XYZ type is yet because it has not defined it yet. Therefore the error given is 'XYZ' does not name a type.
我的问题是我有一个 ABC 班和一个 XYZ 班。ABC 类有一个声明为 XYZ 类型的成员。XYZ 类有一个声明为 ABC 类型的成员。编译器还不知道 XYZ 类型是什么,因为它还没有定义它。因此给出的错误是“XYZ”没有命名类型。
Example Code:
示例代码:
class ABC{
private:
XYZ *xyz; //XYZ is not defined yet
};
class XYZ{
private:
ABC *abc; //ABC is defined above
};
回答by fyngyrz
in a recent QT project, where I had just installed the most recent QT (3/2011), I cured the three of these that was stopping my build by adding this...
在最近的一个 QT 项目中,我刚刚安装了最新的 QT (3/2011),我通过添加这个来解决其中三个阻止我构建的问题......
#include <sys/types.h>
...prior to the include of the header file that was throwing the errors. That did it.
...在包含抛出错误的头文件之前。做到了。
I don't know why they would distribute something that had such problems, perhaps in other systems types.h is included with something else, but not in my case, anyway. Hope that helps someone.
我不知道为什么他们会分发有这些问题的东西,也许在其他系统中 type.h 包含在其他东西中,但无论如何都没有。希望能帮助某人。
回答by TonyK
Two possibilities occur to me:
1. Perhaps you have SLOT instead of SIGNAL in a connect() call.
2. Sometimes it helps to make a gratuitous edit to the .PRO file (e.g. insert and delete a space), so that QMake gets run and the .moc files get generated.
我有两种可能性:
1. 也许你在 connect() 调用中有 SLOT 而不是 SIGNAL。2. 有时对 .PRO 文件进行无偿编辑(例如插入和删除一个空格)会有所帮助,以便 QMake 运行并生成 .moc 文件。
回答by Dirk Eddelbuettel
Does #include'ing the corresponding header file help?
#include'ing 相应的头文件有帮助吗?
回答by aviraldg
If you're using templates, then you need to precede the class name with "typename" so that the compiler can recognize it as a type...
如果您使用模板,那么您需要在类名之前加上“typename”,以便编译器可以将其识别为类型...
template <typename t> //...
回答by NuclearPeon
In my case I wasn't using the namespace the class was defined in. Contents of the header were contained within the namespace, but the source file was missing the using namespace
directive.
在我的例子中,我没有使用定义类的命名空间。标题的内容包含在命名空间中,但源文件缺少using namespace
指令。
.h
:
.h
:
namespace mynamespace {
class MyClass : public QWidget
{
...
}
}
.cpp
:
.cpp
:
using namespace mynamespace
MyClass::MyClass(QWidget *parent) : QWidget(parent)
{
}