c++ - QFile 类嵌套的问题

标签 c++ qt qfile qvector qtextstream

当我定义如下结构时出现问题

struct TInputData
{
    QString      filename;
    QFile        file;
    QTextStream  stream;
};

然后我将其放入 QVector 容器中,如下所示:

QVector<struct TInputData> input(DATA_SOURCE_END);

之后我调用 vector 成员字段的一些方法:

for(int i = 0; i < DATA_SOURCE_END; ++i)
{
    input[i].filename = QString(argv[i + 1]);
    input[i].file.setFileName(input[i].filename);
    if (!input[i].file.open(QIODevice::ReadOnly))
    {
        QDebug(QtCriticalMsg) << "Failed to open input file: " << input[i].filename << "!!!";
        return a.exec();
    }
    input[i].stream.setDevice(&input[i].file);
    qDebug() << "Connected to input file " << input[i].filename;
}

我收到以下编译错误:

/usr/include/qt4/QtCore/qfile.h:209: error: 'QFile::QFile(const QFile&)' is private 
within this context <at line where struct TInputData is declared>

对于 QTextStream 也是如此。

那我错过了什么?

预先感谢您的帮助。

更新

@Ashot 提供的解决方案是手动创建 TInputData 对象。但它引入了一些额外的内存管理困难。解决方法是使用智能指针。

最佳答案

更改 QVector<struct TInputData> input(DATA_SOURCE_END);QVector<TInputData*> input(DATA_SOURCE_END);

QVector需要复制其内容,但复制 QFile 的构造函数是私有(private)的,不能复制。

当使用指针时,您应该手动新建和删除 TInputData 对象,或者您可以为它使用智能指针。

你可以试试QSharedPointer http://qt-project.org/doc/qt-4.8/qsharedpointer.html#QSharedPointer-2 . 您的 vector 将是 QVector<QSharedPointer<TInputData> > input(DATA_SOURCE_END)

关于c++ - QFile 类嵌套的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18997395/

相关文章:

c++ - 用三种颜色 A 和 B 和 C 填充网格

c++ - 回调函数 : difference between void(*func)(int) and void(func)(int)

c++ - 在 Unicode 中转换变量

c++ - Qt5+ 如何为 QMediaPlayer 设置默认音频设备

http - 如何从 QUrl 中获取 "query string"?

c++ - 各种变量的一个标题

c++ - 调整QListView的高度以适应内容

c++ - QFile不读取文件

c++ - 返回 QFile::copy from web

c++ - 为什么 QDir::rmdir 不是静态的?