lupdate 错误:使用未知的命名空间/类进行限定

发布于 2024-11-17 15:09:03 字数 759 浏览 6 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

ㄖ落Θ余辉 2024-11-24 15:09:03

答案是lupdate在解析foo.cpp时无法找到头文件foo.h。使用以下行扩展 .pro 文件消除了问题:

INCLUDEPATH += .

但是,仍然让我有点困扰的是编译器应该无法编译代码,但不知何故,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:

INCLUDEPATH += .

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?

镜花水月 2024-11-24 15:09:03

就我而言,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.

真心难拥有 2024-11-24 15:09:03

我在 Ubuntu 12.04 系统上使用 Qt 5.4.1 时也看到过此错误消息。

但错误的原因不同。 “lupdate”似乎对 C++-11 强枚举有问题,至少在使用封装在命名空间中的相关前向声明时是这样。

我在相关头文件中使用类似的内容:

namespace outer {
namespace other {
  enum class MyEnum : int;
} // namespace outer::other

namespace inner {
   class MyClass {
     //...
   };
} // namespace outer::inner
} // namespace outer

“lupdate”的问题可以通过使用术语“枚举类”的宏来解决:

#ifndef ENUM_CLASS
#define ENUM_CLASS enum class
#endif

namespace outer {
namespace other {
  ENUM_CLASS MyEnum : int;
} // namespace outer::other

namespace inner {
   class MyClass {
     //...
   };
} // namespace outer::inner
} // namespace outer

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:

namespace outer {
namespace other {
  enum class MyEnum : int;
} // namespace outer::other

namespace inner {
   class MyClass {
     //...
   };
} // namespace outer::inner
} // namespace outer

The problem with "lupdate" can be worked around by using a marco for the term "enum class":

#ifndef ENUM_CLASS
#define ENUM_CLASS enum class
#endif

namespace outer {
namespace other {
  ENUM_CLASS MyEnum : int;
} // namespace outer::other

namespace inner {
   class MyClass {
     //...
   };
} // namespace outer::inner
} // namespace outer
恏ㄋ傷疤忘ㄋ疼 2024-11-24 15:09:03

就我而言,在实际的类声明之前,结构中有一个奇怪的(但我猜是合法的)格式化的类型声明。这使得 lupdate 跳过该课程并导致出现“不合格”警告消息。代码如下所示(一开始就不是我的;-)):

struct coords_t {
  double x, y;
  ...
};

struct plotsCollection {
  QVarLengthArray<struct coords_t> coords;  // problem right here
  ...
};

class LogsDialog : public QDialog
{
  Q_OBJECT
  ...
}

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 ;-) ):

struct coords_t {
  double x, y;
  ...
};

struct plotsCollection {
  QVarLengthArray<struct coords_t> coords;  // problem right here
  ...
};

class LogsDialog : public QDialog
{
  Q_OBJECT
  ...
}

Removing the struct from QVarLengthArray<struct coords_t> solved the warning. So did moving the struct declarations inside the class. I did both :)

友欢 2024-11-24 15:09:03

我刚刚在不同的场景中收到了同样的消息。就我而言,问题是 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 made lupdate happy.

梦中的蝴蝶 2024-11-24 15:09:03

如果 tr() 宏放置在嵌套太深的子文件夹中,也会出现此错误。

例如,在下面的示例中,命令
lupdate -noobsolete 。 -ts ts/*
读取错误消息
pwd/a/b/c/c.cpp:5:使用未知的命名空间/类 ::C 进行限定

QT       += core gui

SOURCES += \
    main.cpp \
    a/a.cpp \
    a/b/b.cpp \
    a/b/c/c.cpp

HEADERS += \
    a/a.h \
    a/b/b.h \
    a/b/c/c.h

TRANSLATIONS += \
    ts/foo.ts

然而,正如其他人已经提到的,*.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.

QT       += core gui

SOURCES += \
    main.cpp \
    a/a.cpp \
    a/b/b.cpp \
    a/b/c/c.cpp

HEADERS += \
    a/a.h \
    a/b/b.h \
    a/b/c/c.h

TRANSLATIONS += \
    ts/foo.ts

However, as others have already mentioned, the *.ts-file seems to be updated correctly.

萌吟 2024-11-24 15:09:03

一些前向声明似乎也会导致此警告。
删除此类示例中的“模板类 MutexQList”行可以解决我的案例中的问题:

#include "bufferwriter.h"
#include "resampleworker.h"
#include "worker_help.h"

class Buffer;
class Project;

template <class T> class MutexQList;

class WorkerRegistry : public QObject
{
    Q_OBJECT
private:
    static WorkerRegistry *my_instance;

    MutexQList<BufferWriter *>*bw_list;                 ///< Liste mit registrierten BufferWriter Instanzen
    MutexQList<ResampleWorker *>*rw_list;               ///< Liste mit registrierten ResampleWorker Instanzen

public:
    static WorkerRegistry *instance();
    static WorkerRegistry &ref();
    static void destroy();
}

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:

#include "bufferwriter.h"
#include "resampleworker.h"
#include "worker_help.h"

class Buffer;
class Project;

template <class T> class MutexQList;

class WorkerRegistry : public QObject
{
    Q_OBJECT
private:
    static WorkerRegistry *my_instance;

    MutexQList<BufferWriter *>*bw_list;                 ///< Liste mit registrierten BufferWriter Instanzen
    MutexQList<ResampleWorker *>*rw_list;               ///< Liste mit registrierten ResampleWorker Instanzen

public:
    static WorkerRegistry *instance();
    static WorkerRegistry &ref();
    static void destroy();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文