【发布时间】:2014-12-14 10:29:18
【问题描述】:
我真的尝试了所有方法但找不到解决方案,我尝试先初始化外部 QVector 然后初始化内部但没有成功。
【问题讨论】:
标签: c++ qt qstring qtcore qvector
我真的尝试了所有方法但找不到解决方案,我尝试先初始化外部 QVector 然后初始化内部但没有成功。
【问题讨论】:
标签: c++ qt qstring qtcore qvector
QVector *matrix(作为类成员)带有新的?
这有问题,即:
您不应在堆上分配 QVector(即作为带有 new 的指针)。
你应该更多地使用QStringList。
我个人的建议是这样的:
#include <QVector>
#include <QStringList>
#include <QDebug>
class Foo
{
public:
Foo() { qDebug() << matrix; }
private:
// Could be QStringLiteral, but you could also build it in the
// constructor if it is dynamic
QVector<QStringList> matrix{{"foo", "bar", "baz"}, {"hello", "world", "!"}};
};
int main()
{
Foo foo;
return 0;
}
TEMPLATE = app
TARGET = main
QT = core
CONFIG += c++11
SOURCES += main.cpp
qmake && make && ./main
QVector(("foo", "bar", "baz"), ("hello", "world", "!"))
【讨论】:
可能你不能这样做,因为你错过了一个>。所以试试这个:
#include<QDebug>
//...
private:
QVector<QVector<QString> > *matrix = new QVector<QVector<QString> >;
在构造函数中:
matrix->append(QVector<QString>() << "hello world");
qDebug() << "output: " << *matrix;
但我认为你应该在构造函数中分配内存。例如:
private:
QVector<QVector<QString> > *matrix;
在构造函数中:
matrix = new QVector<QVector<QString> >;
matrix->append(QVector<QString>() << "hello world");
qDebug() << "output:" << *matrix;
两种情况下的输出:
输出:QVector(QVector("hello world"))
【讨论】: