c++ - 'sizeof' 对 QGlobal.h 中不完整类型 'QStaticAssertFailure<false>' 的无效应用

标签 c++ qt sizeof

我目前正在将一个巨大的项目从 Qt 4.x 迁移到 5.2.1,在出现这个错误之前一切都很好,我发现它非常令人困惑,因为它位于 Qt 文件中,我相信必须应用解决方案在其他地方,不在位于 5.2.1\mingw48_32\include/QtCore/qglobal.h 的 qglobal.h 中。 错误一定发生在其他地方。

这里是错误:

..........\Qt5\5.2.1\mingw48_32\include/QtCore/qglobal.h:681:85: error: invalid application of 'sizeof' to incomplete type 'QStaticAssertFailure' enum {Q_STATIC_ASSERT_PRIVATE_JOIN(q_static_assert_result, COUNTER) = sizeof(QStaticAssertFailure)} ^

..........\Qt5\5.2.1\mingw48_32\include/QtCore/qglobal.h:686:47: note: in expansion of macro 'Q_STATIC_ASSERT' #define Q_STATIC_ASSERT_X(Condition, Message) Q_STATIC_ASSERT(Condition) ^

..........\Qt5\5.2.1\mingw48_32\include/QtCore/qobject.h:520:5: note: in expansion of macro 'Q_STATIC_ASSERT_X' Q_STATIC_ASSERT_X(QtPrivate::HasQ_OBJECT_Macro::Value, ^

这是qglobal.h中的一段代码

// Intentionally undefined
template <bool Test> class QStaticAssertFailure;
template <> class QStaticAssertFailure<true> {};

#define Q_STATIC_ASSERT_PRIVATE_JOIN(A, B) Q_STATIC_ASSERT_PRIVATE_JOIN_IMPL(A, B)
#define Q_STATIC_ASSERT_PRIVATE_JOIN_IMPL(A, B) A ## B
#ifdef __COUNTER__
#define Q_STATIC_ASSERT(Condition) \
    enum {Q_STATIC_ASSERT_PRIVATE_JOIN(q_static_assert_result, __COUNTER__) = sizeof(QStaticAssertFailure<!!(Condition)>)}
#else
#define Q_STATIC_ASSERT(Condition) \
    enum {Q_STATIC_ASSERT_PRIVATE_JOIN(q_static_assert_result, __LINE__) = sizeof(QStaticAssertFailure<!!(Condition)>)}
#endif /* __COUNTER__ */
#define Q_STATIC_ASSERT_X(Condition, Message) Q_STATIC_ASSERT(Condition)
#endif

我已经尝试了一切并研究了所有相关内容,但没有找到合适的解决方案。我在这段烦人的代码中浪费了一整天。我希望有人能对此事有所了解。

非常感谢。

编辑:我搜索了所有包含 qglobal.h 的文档,但没有一个使用任何断言函数,所以我不知道是什么触发了此类错误。 qglobal.h不可能是错误的,所以它一定是源代码的东西。

EDIT2:我设法隔离了触发错误的行,显然编译输出提供了比我想象的更多的信息,但它是如此“分离”以至于我认为这是一个警告并且无事可做。这是我的应用程序中的代码。两条注释行是触发错误的行。对不起我的错误。

bool ISPSModel::removeGraphics(GraphicsPrimitive* _gtr) {

    for (int _i = 0; _i < ispss.size(); _i++) {

        for (int _j = 0; _j < ispss[_i]->graphicsObjects.size(); _j++) {

            if (ispss[_i]->graphicsObjects[_j] != _gtr)
                continue;

            if (ispss[_i]->graphicsObjects.contains(_gtr)) {

                //beginRemoveRows(indexFromItem(ispss[_i]->m_item), _gtr->getData(DATA_ROLE).value<ISPSItem*>()->row(), _gtr->getData(DATA_ROLE).value<ISPSItem*>()->row());
               //_gtr->getData(DATA_ROLE).value<ISPSItem*>()->remove();
                ispss[_i]->removeGraphics(ispss[_i]->graphicsObjects[_j]);
                endRemoveRows();
                return true;
            }
        }
    }

    return false;
}

这是导致错误的部分:

value<ISPSItem*>()

这是我忽略的编译错误的另一部分,希望它能有所帮助:

..........\Qt5\5.2.1\mingw48_32\include/QtCore/qobject.h: In instantiation of 'T qobject_cast(QObject*) [with T = ISPSItem*]': ..........\Qt5\5.2.1\mingw48_32\include/QtCore/qvariant.h:695:51: required from 'static T QtPrivate::QVariantValueHelper::object(const QVariant&) [with T = ISPSItem*]' ..........\Qt5\5.2.1\mingw48_32\include/QtCore/qvariant.h:101:37: required from 'static ReturnType QtPrivate::ObjectInvoker::invoke(Argument) [with Derived = QtPrivate::QVariantValueHelper; Argument = const QVariant&; ReturnType = ISPSItem*]' ..........\Qt5\5.2.1\mingw48_32\include/QtCore/qvariant.h:810:64: required from 'T qvariant_cast(const QVariant&) [with T = ISPSItem*]' ..........\Qt5\5.2.1\mingw48_32\include/QtCore/qvariant.h:348:36: required from 'T QVariant::value() const [with T = ISPSItem*]' ..\marssies\ispswidget.cpp:785:109: required from here EDIT3: I left those 2 lines commented and kept migrating the application, until I got the same error in another file, this is the line:

我在另一个使用不同类的 .cpp 中遇到了同样的错误,但它们的共同点是 .value<Type>();

Notify* _n = ui.notifyBox->model()->data(index, Qt::UserRole).value<Notify*>();

所以绝对是 .value<Type>();是什么引发了错误,现在唯一剩下的就是找到解决方法。

这里是对象 ISPSItem,以防它有用:

class ISPSItem : public QObject {

public:
    enum Level {ROOT_LEVEL = 1,
                    ISPS_LEVEL,
                        GRAPHICS_LEVEL
                    } level;

    ISPSItem(ISPSItem* = NULL, Level = ROOT_LEVEL, int = -1);
    ~ISPSItem();

    ISPSItem* parentItem() {return _parentItem;}

    ISPSItem* child(int);
    void appendChild(ISPSItem*);
    void insertChild(ISPSItem*, int);
    int childCount() const {return children.size();}

    int row() const;
    int newNodeRow(Level);
    void remove();

private:
    QList<ISPSItem*> children;
    ISPSItem* _parentItem;
    void remove(ISPSItem*);
};
Q_DECLARE_METATYPE(ISPSItem*)

最佳答案

在获得有关错误和触发它的来源的大量信息后,我得出结论,根本原因可能是在 QVariant 中使用 QObject 子类,尤其是在调用 QVariant::value( ) 函数。正如 Qt 文档所说:

If the QVariant contains a pointer to a type derived from QObject then T may be any QObject type. If the pointer stored in the QVariant can be qobject_cast to T, then that result is returned. Otherwise a null pointer is returned. Note that this only works for QObject subclasses which use the Q_OBJECT macro.

我相信,向这些类 (ISPSItem) 声明添加 Q_OBJECT 宏将解决问题。

关于c++ - 'sizeof' 对 QGlobal.h 中不完整类型 'QStaticAssertFailure<false>' 的无效应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23244805/

相关文章:

c++ - QVector(or std::vector) reserve(or resize) down 不释放内存

python - 使用 QClipboard,如何复制富文本并将其降级为文本编辑器的纯文本?

c - 计算C中的字符串长度

c++ - 在循环中添加预期的调用

c++ - 调试取决于所选调度程序的奇怪错误

c++ - 为什么我的 C++ 实现比 R 源代码慢?

c++ - toString() : numbers in JSON

c++ - 使用 qt 中的事件将项目属性传递给主窗口

c++ - 为什么 C 字 rune 字是 int 而不是 char?

c++ - 在所有架构上 sizeof(float) 总是等于 sizeof(int) 吗?