错误:请求从"QStringList"转换为非标量类型"QString"
error: conversion from 'QStringList' to non-scalar type 'QString' requested
这是我的类:
// file .h
#ifndef UNDOREDO_H
#define UNDOREDO_H
#include <QUndoCommand>
typedef QVector<QStringList> vector_t ;
class UndoRedo : public QUndoCommand
{
public:
UndoRedo(QList<vector_t> v,
QUndoCommand *parent = 0);
void undo();
private:
QList<vector_t> *cb_values;
};
#endif // UNDOREDO_H
// file .cpp
#include "undoredo.h"
UndoRedo::UndoRedo(QList<vector_t> v,
QUndoCommand *parent)
: QUndoCommand(parent)
{
cb_values = &v;
}
void UndoRedo::undo() {
QString last = cb_values[0][0].takeLast();
qDebug() << last << "removed!";
}
当我调用undo()方法时,IDE会抛出这个错误:
错误:请求从'QStringList'转换为'QString'非标量类型
我哪里做错了?
在你的构造函数中,你接受一个参数的地址,当构造函数返回时该参数将消失:
cb_values = &v;
这行可以编译,但是没有意义。一旦构造函数返回,存储在cb_values
中的指针就会悬空,并且它的进一步使用可能会导致,嗯,硬盘被格式化。
让我们分解cb_values[0][0].takeLast()
QList<vector_t> * cb_values
QList<vector_t> cb_values[0]
QVector<QStringList>=vector_t cb_values[0][0]
QStringList cb_values[0][0].takeLast()
因此,表达式的类型是QStringList
,但您试图将其分配给QString
。我不知道你真正想要的是什么。也许是(*cb_values)[0][0].takeLast()
?
cb_values
是指向QList<vector_t>
的指针,所以cb_values[0]
是QList<vector_t>
。所以cb_values[0]
就是vector_t
或者QVector<QStringList>
。然后在这个向量上调用takeLast()
,它返回QStringList
,您尝试将其赋值给QString
。在我看来,你正在调用takeLast()
而不是你想要的对象
相关文章:
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 为什么 std::optional::operator=(U&&) 要求你是非标量类型?
- 错误:请求从"常量字符 [5]"转换为非标量类型"字符串"
- 特征中的自定义标量类型
- 标量类型的特征模板无法编译固定大小的子矩阵操作
- 请求从"点*"转换为非标量类型"点"
- 标量类型上的特征模板
- 错误:在 C++ 中从 'Counter' 转换为非标量类型'CountDn'
- 特征:从数组类型中获取标量类型是否记录?
- 错误:请求从"Mstream"转换为非标量类型"std::string {aka std::basic_string<char>}"
- Arduino trim() 和 replace() 方法返回从 'void' 到非标量类型'String'请求的转换
- 错误:移动文件时'QString'为非标量类型"std::__cxx11::字符串"
- 标量类型附加C++
- 复制初始化 - 从 'int' 类型转换为非标量类型
- 在 opencv 上将 vec 类型转换为标量类型
- 为矢量对象请求转换为非标量类型
- 错误:请求从 std::chrono::time_point 浮点数转换为非标量类型长整型
- 此错误的含义是什么:从 'char* [40] 转换为非标量类型 'std::string
- 小标量类型和谷歌协议缓冲区
- 我可以强制执行标量类型通过 int{} 初始化为零吗?