当前位置:   article > 正文

Qt信号参数中使用QVariantList时编译问题_(reinterpret_cast(current))->~t();

(reinterpret_cast(current))->~t();

今天调试代码时遇到一个奇怪的问题,不过一般感觉比较奇怪的问题,最后查到原因时,原因都比较简单!

编译问题

先来看一下qt的编译错误,提示一堆错误:

  1. In file included from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qglobal.h:1173:0,
  2. from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qnamespace.h:43,
  3. from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qobjectdefs.h:48,
  4. from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/qobject.h:46,
  5. from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/QObject:1,
  6. from temp\moc\../../../test/module_function/test.h:4,
  7. from temp\moc\moc_test.cpp:9:
  8. D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qtypeinfo.h: In instantiation of 'class QTypeInfo<QVariant>':
  9. D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qlist.h:459:31: required from 'void QList<T>::node_copy(QList<T>::Node*, QList<T>::Node*, QList<T>::Node*) [with T = QVariant]'
  10. D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qlist.h:813:22: required from 'QList<T>::QList(const QList<T>&) [with T = QVariant]'
  11. temp\moc\moc_test.cpp:103:82: required from here
  12. D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qtypeinfo.h:67:26: error: invalid application of 'sizeof' to incomplete type 'QVariant'
  13. isLarge = (sizeof(T)>sizeof(void*)),
  14. ^
  15. D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qtypeinfo.h:69:24: error: invalid application of 'sizeof' to incomplete type 'QVariant'
  16. sizeOf = sizeof(T)
  17. ^
  18. In file included from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/qobject.h:49:0,
  19. from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/QObject:1,
  20. from temp\moc\../../../test/module_function/test.h:4,
  21. from temp\moc\moc_test.cpp:9:
  22. D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qlist.h: In instantiation of 'void QList<T>::node_copy(QList<T>::Node*, QList<T>::Node*, QList<T>::Node*) [with T = QVariant]':
  23. D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qlist.h:813:22: required from 'QList<T>::QList(const QList<T>&) [with T = QVariant]'
  24. temp\moc\moc_test.cpp:103:82: required from here
  25. D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qlist.h:462:28: error: invalid use of incomplete type 'class QVariant'
  26. current->v = new T(*reinterpret_cast<T*>(src->v));
  27. ^
  28. In file included from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/qobject.h:54:0,
  29. from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/QObject:1,
  30. from temp\moc\../../../test/module_function/test.h:4,
  31. from temp\moc\moc_test.cpp:9:
  32. D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qmetatype.h:114:21: note: forward declaration of 'class QVariant'
  33. F(QVariant, 41, QVariant) \
  34. ^
  35. D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qmetatype.h:1924:11: note: in definition of macro 'QT_FORWARD_DECLARE_STATIC_TYPES_ITER'
  36. class Name;
  37. ^
  38. D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qmetatype.h:1926:1: note: in expansion of macro 'QT_FOR_EACH_STATIC_CORE_CLASS'
  39. QT_FOR_EACH_STATIC_CORE_CLASS(QT_FORWARD_DECLARE_STATIC_TYPES_ITER)
  40. ^
  41. In file included from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/qobject.h:49:0,
  42. from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/QObject:1,
  43. from temp\moc\../../../test/module_function/test.h:4,
  44. from temp\moc\moc_test.cpp:9:
  45. D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qlist.h:468:17: warning: possible problem detected in invocation of delete operator: [-Wdelete-incomplete]
  46. delete reinterpret_cast<T*>(current->v);
  47. ^
  48. D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qlist.h:468:17: warning: invalid use of incomplete type 'class QVariant'
  49. In file included from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/qobject.h:54:0,
  50. from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/QObject:1,
  51. from temp\moc\../../../test/module_function/test.h:4,
  52. from temp\moc\moc_test.cpp:9:
  53. D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qmetatype.h:114:21: note: forward declaration of 'class QVariant'
  54. F(QVariant, 41, QVariant) \
  55. ^
  56. D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qmetatype.h:1924:11: note: in definition of macro 'QT_FORWARD_DECLARE_STATIC_TYPES_ITER'
  57. class Name;
  58. ^
  59. D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qmetatype.h:1926:1: note: in expansion of macro 'QT_FOR_EACH_STATIC_CORE_CLASS'
  60. QT_FOR_EACH_STATIC_CORE_CLASS(QT_FORWARD_DECLARE_STATIC_TYPES_ITER)
  61. ^
  62. In file included from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/qobject.h:49:0,
  63. from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/QObject:1,
  64. from temp\moc\../../../test/module_function/test.h:4,
  65. from temp\moc\moc_test.cpp:9:
  66. D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qlist.h:468:17: note: neither the destructor nor the class-specific operator delete will be called, even if they are declared when the class is defined
  67. delete reinterpret_cast<T*>(current->v);
  68. ^
  69. D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qlist.h:475:17: error: invalid use of incomplete type 'class QVariant'
  70. new (current) T(*reinterpret_cast<T*>(src));
  71. ^
  72. In file included from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/qobject.h:54:0,
  73. from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/QObject:1,
  74. from temp\moc\../../../test/module_function/test.h:4,
  75. from temp\moc\moc_test.cpp:9:
  76. D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qmetatype.h:114:21: note: forward declaration of 'class QVariant'
  77. F(QVariant, 41, QVariant) \
  78. ^
  79. D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qmetatype.h:1924:11: note: in definition of macro 'QT_FORWARD_DECLARE_STATIC_TYPES_ITER'
  80. class Name;
  81. ^
  82. D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qmetatype.h:1926:1: note: in expansion of macro 'QT_FOR_EACH_STATIC_CORE_CLASS'
  83. QT_FOR_EACH_STATIC_CORE_CLASS(QT_FORWARD_DECLARE_STATIC_TYPES_ITER)
  84. ^
  85. In file included from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/qobject.h:49:0,
  86. from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/QObject:1,
  87. from temp\moc\../../../test/module_function/test.h:4,
  88. from temp\moc\moc_test.cpp:9:
  89. D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qlist.h:481:17: error: invalid use of incomplete type 'class QVariant'
  90. (reinterpret_cast<T*>(current))->~T();
  91. ^
  92. In file included from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/qobject.h:54:0,
  93. from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/QObject:1,
  94. from temp\moc\../../../test/module_function/test.h:4,
  95. from temp\moc\moc_test.cpp:9:
  96. D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qmetatype.h:114:21: note: forward declaration of 'class QVariant'
  97. F(QVariant, 41, QVariant) \
  98. ^
  99. D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qmetatype.h:1924:11: note: in definition of macro 'QT_FORWARD_DECLARE_STATIC_TYPES_ITER'
  100. class Name;
  101. ^
  102. D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qmetatype.h:1926:1: note: in expansion of macro 'QT_FOR_EACH_STATIC_CORE_CLASS'
  103. QT_FOR_EACH_STATIC_CORE_CLASS(QT_FORWARD_DECLARE_STATIC_TYPES_ITER)
  104. ^
  105. In file included from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/qobject.h:49:0,
  106. from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/QObject:1,
  107. from temp\moc\../../../test/module_function/test.h:4,
  108. from temp\moc\moc_test.cpp:9:
  109. D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qlist.h: In instantiation of 'void QList<T>::node_destruct(QList<T>::Node*, QList<T>::Node*) [with T = QVariant]':
  110. D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qlist.h:865:18: required from 'void QList<T>::dealloc(QListData::Data*) [with T = QVariant]'
  111. D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qlist.h:827:16: required from 'QList<T>::~QList() [with T = QVariant]'
  112. temp\moc\moc_test.cpp:103:82: required from here
  113. D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qlist.h:494:31: warning: possible problem detected in invocation of delete operator: [-Wdelete-incomplete]
  114. while(from != to) --to, delete reinterpret_cast<T*>(to->v);
  115. ^
  116. D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qlist.h:494:31: warning: invalid use of incomplete type 'class QVariant'
  117. In file included from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/qobject.h:54:0,
  118. from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/QObject:1,
  119. from temp\moc\../../../test/module_function/test.h:4,
  120. from temp\moc\moc_test.cpp:9:
  121. D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qmetatype.h:114:21: note: forward declaration of 'class QVariant'
  122. F(QVariant, 41, QVariant) \
  123. ^
  124. D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qmetatype.h:1924:11: note: in definition of macro 'QT_FORWARD_DECLARE_STATIC_TYPES_ITER'
  125. class Name;
  126. ^
  127. D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qmetatype.h:1926:1: note: in expansion of macro 'QT_FOR_EACH_STATIC_CORE_CLASS'
  128. QT_FOR_EACH_STATIC_CORE_CLASS(QT_FORWARD_DECLARE_STATIC_TYPES_ITER)
  129. ^
  130. In file included from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/qobject.h:49:0,
  131. from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/QObject:1,
  132. from temp\moc\../../../test/module_function/test.h:4,
  133. from temp\moc\moc_test.cpp:9:
  134. D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qlist.h:494:31: note: neither the destructor nor the class-specific operator delete will be called, even if they are declared when the class is defined
  135. while(from != to) --to, delete reinterpret_cast<T*>(to->v);
  136. ^
  137. D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qlist.h:496:32: error: invalid use of incomplete type 'class QVariant'
  138. while (from != to) --to, reinterpret_cast<T*>(to)->~T();
  139. ^
  140. In file included from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/qobject.h:54:0,
  141. from D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtCore/QObject:1,
  142. from temp\moc\../../../test/module_function/test.h:4,
  143. from temp\moc\moc_test.cpp:9:
  144. D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qmetatype.h:114:21: note: forward declaration of 'class QVariant'
  145. F(QVariant, 41, QVariant) \
  146. ^
  147. D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qmetatype.h:1924:11: note: in definition of macro 'QT_FORWARD_DECLARE_STATIC_TYPES_ITER'
  148. class Name;
  149. ^
  150. D:\Qt\Qt5.10.0\5.10.0\mingw53_32\include/QtCore/qmetatype.h:1926:1: note: in expansion of macro 'QT_FOR_EACH_STATIC_CORE_CLASS'
  151. QT_FOR_EACH_STATIC_CORE_CLASS(QT_FORWARD_DECLARE_STATIC_TYPES_ITER)
  152. ^
  153. Makefile.Debug:3709: recipe for target 'temp/obj/moc_test.o' failed
  154. mingw32-make[1]: Leaving directory 'E:/code/test-Debug'
  155. Makefile:36: recipe for target 'debug' failed
  156. mingw32-make[1]: *** [temp/obj/moc_test.o] Error 1
  157. mingw32-make: *** [debug] Error 2
  158. 18:04:40: 进程"D:\Qt\Qt5.10.0\Tools\mingw530_32\bin\mingw32-make.exe"退出,退出代码 2
  159. Error while building/deploying project test (kit: Desktop Qt 5.10.0 MinGW 32bit)
  160. When executing step "Make"

排查问题

从这里面可以看出,基本上是与QVariant类型的使用有关系,查了下代码,里面没有直接使用QVariant类型的地方,到是有一个信号的参数类型,使用的是QVariantList,怀疑有可能是这个信号引起的,把这信号注释掉之后,编译不在报错。然后类里面的成员变量里使用QVariantList定义的成员变量,则不会导致这个编译错误!

另外,通过查看编译错误,可以发现,是编译moc_test.cpp文件时开始报错的,也就是说,在编译moc文件时提示报错,moc文件里第9行是#include " temp\moc\../../../test/module_function/test.h",引用的是测试类的头文件,也看不出什么错误。

只是尝试给test.h里加了个头文件引用:#include <QVariantList>,再次编译错误没有了,编译通过!

 

问题原因

使用QVariantList作为信号的参数时,一定引用头文件:

#include  <QVariantList>

否则会编译错误,如果不作为信号的参数,只是作为成员变量用,或者成员函数的参数,不引用其头文件也不会报错。

这两种情况编译是有点区别的,moc文件是qt编译器根据类源文件自动生成的代码。

 

在此记录一下,以免以后调试遇到同样的问题而浪费时间。

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/121193
推荐阅读
相关标签
  

闽ICP备14008679号