Qt在QVariant抛出非法错误
Qt Throwing Illegal Error at QVariant
我有一些代码旨在从一组数据中获取QMaps的QList。然后它应该遍历该列表中的所有 QMaps。由于某种原因,在尝试此操作时,我收到一些有关用于存储数据的QVector的错误。这是我的代码:
#include <QCoreApplication>
#include <QMap>
#include <QVariant>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QList<QMap<QString, QVariant>> maps;
QMap<QString, QVariant> item1;
item1.insert("Item 1", QVariant::fromValue(1));
maps.append(item1);
foreach (const QMap<QString, QVariant> & map, maps)
{
qDebug() << map;
}
return a.exec();
}
错误:
..ErrorTestmain.cpp(17): warning C4002: too many actual parameters for macro 'Q_FOREACH'
..ErrorTestmain.cpp(17): error C2275: 'QVariant': illegal use of this type as an expression
C:Qt5.8msvc2015_64includeQtCore/qsharedpointer_impl.h(94): note: see declaration of 'QVariant'
..ErrorTestmain.cpp(17): error C2955: 'std::remove_reference': use of class template requires template argument list
D:Microsoft Visual Studio 14.0VCINCLUDExtr1common(301): note: see declaration of 'std::remove_reference'
..ErrorTestmain.cpp(17): error C2065: 'map': undeclared identifier
..ErrorTestmain.cpp(17): error C2143: syntax error: missing ')' before '>'
..ErrorTestmain.cpp(17): error C2059: syntax error: '>'
..ErrorTestmain.cpp(17): error C2065: '_container_': undeclared identifier
..ErrorTestmain.cpp(17): error C2228: left of '.control' must have class/struct/union
..ErrorTestmain.cpp(17): note: type is 'unknown-type'
..ErrorTestmain.cpp(17): error C2228: left of '.i' must have class/struct/union
..ErrorTestmain.cpp(17): note: type is 'unknown-type'
..ErrorTestmain.cpp(17): error C2228: left of '.e' must have class/struct/union
..ErrorTestmain.cpp(17): note: type is 'unknown-type'
..ErrorTestmain.cpp(17): error C2059: syntax error: ')'
..ErrorTestmain.cpp(17): error C2143: syntax error: missing ';' before 'for'
..ErrorTestmain.cpp(17): error C2059: syntax error: '='
..ErrorTestmain.cpp(17): error C2143: syntax error: missing ';' before '}'
..ErrorTestmain.cpp(17): fatal error C1004: unexpected end-of-file found
谢谢,是的,tagData->toMappedList()
返回正确的 QMaps/数据集。
恕
我直言,问题是模板内部的,
。可悲的是,foreach
只不过是对Q_FOREACH
的呼吁,这是一个宏。它需要用逗号分隔的参数。但是由于模板,您有 2 个逗号。不久前我遇到了这个问题,尽管Qt Creator至少为我提供了error: macro "Q_FOREACH" passed 3 arguments, but takes just 2
而不是您遇到的大量错误。在您的情况下,这将是:
..ErrorTestmain.cpp(17): warning C4002: too many actual parameters for macro 'Q_FOREACH'
我建议使用迭代器进行for
循环,以遍历映射列表。除非您想转储const
在这种情况下,您可以执行以下操作:
QMap<QString, QVariant> map;
foreach (map, maps) {
...
}
您也可以使用auto
但再次 - 没有恒定性。
如果常量是您的情况下的一个关键方面,请使用常量迭代器的for
循环。
相关文章:
- 编写代码时C++出现错误:错误 1 错误 C2601:'circle':本地函数定义是非法的
- WinLamb 错误:成员初始化非法
- 来自简单循环的 OpenAcc 错误:内核执行期间的非法地址
- 为什么代码会抛出非法内存访问错误
- Qt在QVariant抛出非法错误
- 结构内部的结构:"对非静态成员的非法引用"错误
- 在Ubuntu 14.04启动我的应用程序时,非法说明(核心转储)错误,如何查看转储
- 视觉C++错误 C2451 条件字符串是非法的
- 错误:遇到非法内存访问
- 咖啡错误 == cuda成功(77 与 0)遇到非法内存访问
- CPP(15): 错误 C2182:"输入":"非法使用类型"void"
- 获取列表的第一个和最后一个元素<string>给我非法指令错误
- 错误 C2234:引用数组是非法的
- 错误 C2601:"Name":本地函数定义是非法的
- VC++ SFINAE 给出错误 C2070:"重载函数":操作数大小非法
- 错误 C2296:'<<':非法,左操作数的类型为 'const char [41]'
- qvector.h(74):错误 C2182:'t':非法使用类型 'void'
- 错误 C2275 RHandle:非法使用此类型作为表达式
- 使用mpopcnt编译会导致非法指令错误
- 错误 C2296:'%':非法的左操作数在 C++ 中具有类型 'double'