lupdate 错误:使用未知的命名空间/类进行限定
我使用 lupdate v4.7.2 遇到了一个非常奇怪的错误。我收到错误消息
module/foo.cpp:6:使用未知的命名空间/类 ::foo 进行限定
来限定具有大约 50 个类的项目中的几个类。我将问题归结为一个简单的示例:
src/project.pro:
QT += core
TARGET = test
TEMPLATE = app
SOURCES += main.cpp \
screen.cpp
HEADERS += screen.h
TRANSLATIONS += de.ts
src/module/foo.h:
namespace sp {
class foo {
initWidgets();
};
} // namespace sp
src/module/foo.cpp:
#include <QString>
#include "module/foo.h"
namespace sp {
foo::initWidgets() {
QString bar = tr("bar");
}
} // namespace sp
main.cpp 中有一个空的 main 函数。
代码可以编译(除非我在这里可能产生任何 copypasta 错误),所以语法基本上是正确的。
I ran into a very peculiar error using lupdate v4.7.2. I got the error message
module/foo.cpp:6: Qualifying with unknown namespace/class ::foo
for a couple of classes in a project with about 50 classes. I boiled the problem down to a simple example:
src/project.pro:
QT += core
TARGET = test
TEMPLATE = app
SOURCES += main.cpp \
screen.cpp
HEADERS += screen.h
TRANSLATIONS += de.ts
src/module/foo.h:
namespace sp {
class foo {
initWidgets();
};
} // namespace sp
src/module/foo.cpp:
#include <QString>
#include "module/foo.h"
namespace sp {
foo::initWidgets() {
QString bar = tr("bar");
}
} // namespace sp
main.cpp has an empty main function in it.
The code compiles (barring any copypasta mistakes I might have produced here), so the syntax is basically correct.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
答案是lupdate在解析foo.cpp时无法找到头文件foo.h。使用以下行扩展 .pro 文件消除了问题:
但是,仍然让我有点困扰的是编译器应该无法编译代码,但不知何故,qmake 添加了一个
-I.
到编译器选项。这就是为什么我之前没有想到包含文件问题,并花了几个小时来解决这个问题。有谁知道这是否是默认行为?另外:为什么 lupdate 不发出适当的错误消息?The answer was that lupdate was unable to locate the header file foo.h while parsing foo.cpp. Extending the .pro file with the following line removed the issue:
However, what still bothers me a bit is that the compiler should have been unable to compile the code, but somehow, qmake added a
-I.
to the compiler options. That's why I didn't think of an include file issue earlier and spent a couple of hours puzzling this out. Does anyone know whether this is default behavior? Also: Why does lupdate not emit an appropriate error message?就我而言,lupdate 无法解析 enum MyEnum:int {};线并未能达到类声明。 Qt 5.7.1,lupdate 仍然不理解枚举的类型说明符。
In my case lupdate wasn't able to parse enum MyEnum:int {}; line and failed to reach class declaration. Qt 5.7.1, lupdate still does not understand type specifiers for enums.
我在 Ubuntu 12.04 系统上使用 Qt 5.4.1 时也看到过此错误消息。
但错误的原因不同。 “lupdate”似乎对 C++-11 强枚举有问题,至少在使用封装在命名空间中的相关前向声明时是这样。
我在相关头文件中使用类似的内容:
“lupdate”的问题可以通过使用术语“枚举类”的宏来解决:
I have seen this error message too using Qt 5.4.1 on an Ubuntu 12.04 system.
But the reason for the error was different. "lupdate" seems to have problems with C++-11 strong enums, at least when using related forward declarations encapsulated in a namespace.
I'm using something like this in the related header file:
The problem with "lupdate" can be worked around by using a marco for the term "enum class":
就我而言,在实际的类声明之前,结构中有一个奇怪的(但我猜是合法的)格式化的类型声明。这使得
lupdate
跳过该课程并导致出现“不合格”警告消息。代码如下所示(一开始就不是我的;-)):从
QVarLengthArray
中删除struct
解决了警告。在类内部移动结构声明也是如此。我都做了:)In my case there was a strangely (but legally I guess) formatted type declaration in a struct before the actual class declaration. This made
lupdate
skip the class and resulted in the "unqualified" warning message. The code looked like this (wasn't mine to begin with ;-) ):Removing the
struct
fromQVarLengthArray<struct coords_t>
solved the warning. So did moving the struct declarations inside the class. I did both :)我刚刚在不同的场景中收到了同样的消息。就我而言,问题是
foo.cpp
包含#include
而不是#include "foo.h"
。由于这两个文件都是不在全局包含路径中的目录,因此由于包含中缺少引号,lupdate
无需费心查找该目录。切换到正确的包含分隔符让lupdate
很高兴。I just got this same message in a different scenario. In my case, the issue was that
foo.cpp
contained#include <foo.h>
instead#include "foo.h"
. Since both files were a directory that wasn't in the global include path,lupdate
didn't bother looking in that directory due to the lack of quotation marks in the include. Switching to the proper include delimiters madelupdate
happy.如果
tr()
宏放置在嵌套太深的子文件夹中,也会出现此错误。例如,在下面的示例中,命令
lupdate -noobsolete 。 -ts ts/*
读取错误消息
pwd/a/b/c/c.cpp:5:使用未知的命名空间/类 ::C 进行限定
。然而,正如其他人已经提到的,
*.ts
文件似乎已正确更新。This error occurs also if the
tr()
-macro is placed in too deeply nested subfolders.E.g., in the following example, the command
lupdate -noobsolete . -ts ts/*
error message reads
pwd/a/b/c/c.cpp:5: Qualifying with unknown namespace/class ::C
.However, as others have already mentioned, the
*.ts
-file seems to be updated correctly.一些前向声明似乎也会导致此警告。
删除此类示例中的“模板类 MutexQList”行可以解决我的案例中的问题:
Some forward declarations seem to cause this warning too.
Removing the 'template class MutexQList' line in this class example solves the issue in my case: