英文:
what is the correct way to use vector of vectors to create class buffer? or how to pass it correctly through functions?
问题
我正在编写一个类,用于将文件中的同步数据写入缓冲区。这里的缓冲区是向量的向量。
在头文件中,当我尝试:
private:
QVector<QVector<int>> m_buffer(10); // 我想要一个包含 10 个向量的数组。
它无法通过编译,显示错误:
error: expected identifier before numeric constant
QVector<QVector<int>> m_buffer(10);
^~
在头文件中的函数部分也出现相同的错误,当我尝试:
public:
void getDataFromFiles(QList<QString> audioFilesList, QVector<QVector<int>> buffer(10));
但是当我只保留以下函数签名时,它可以通过编译:
void getDataFromFiles(QList<QString> audioFilesList, QVector<QVector<int>> &buffer);
但在这种情况下,缓冲区没有按我期望的方式输入,我发现所有 10 个向量的数据都是相同的。
在头文件中:
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void getDataFromFiles(QList<QString> audioFilesList, QVector<QVector<int>> &buffer);
private:
Ui::MainWindow *ui;
// QVector<QVector<int>> buffer(10); // 这样不会通过编译
};
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QList<QString> audioFilesList;
audioFilesList.append("/path/to/file1");
audioFilesList.append("/path/to/file2");
QVector<QVector<int>> buffer(10);
getDataFromFiles(audioFilesList, buffer); // 我觉得有一种方法可以传递 buffer(10) 吗?
}
void MainWindow::getDataFromFiles(QList<QString> audioFilesList, QVector<QVector<int>> &buffer)
{
// ...
// 填充缓冲区我使用
buffer[i].append(value);
// ...
}
英文:
I am writing a class to write a sync data from files to buffers. The buffer here is vector of vectors.
in the Header file when I tried
private:
QVector<QVector<int>> m_buffer(10); // I want to have an array of 10 vectors.
It failed to compile with the error:
error: expected identifier before numeric constant
QVector<QVector<int>> m_buffer(10);
^~
and the same error for the function when I tried in the header :
public:
void getDataFromFiles(QList<QString> audioFilesList, QVector<QVector<int> > buffer(10));
But it compiles when I just keep void getDataFromFiles(QList<QString> audioFilesList, QVector<QVector<int> > buffer);
for the function but in this case the buffers are not entered correctly as I want, I found that the data is the same for all 10 vectors.
the header:
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void getDataFromFiles(QList<QString> audioFilesList, QVector<QVector<int> > buffer);
private:
Ui::MainWindow *ui;
// QVector<QVector<int>> buffer(10); // this does not compile
};
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QList<QString> audioFilesList;
audioFilesList.append("/path/to/file1");
audioFilesList.append("/path/to/file2");
QVector<QVector<int>> buffer(10);
getDataFromFiles(audioFilesList, buffer); // I think there is a way to pass buffer(10) instead?
}
void MainWindow::getDataFromFiles(QList<QString> audioFilesList, QVector<QVector<int> > buffer)
{
// ...
// to fill buffer I use
buffer[i].append(value);
// ...
}
</details>
# 答案1
**得分**: 2
初始化如果是C++11或更高版本:
```cpp
QVector<QVector<int>> m_buffer = QVector<QVector<int>>(10);
或者在m_buffer
类的构造函数中:
QVector<QVector<int>> m_buffer;
Constructor(): m_buffer(10){}
最有可能的错误原因是编译器将m_buffer
视为返回类型为QVector<QVector<int>>
的函数,正如@Marek R所提到的。
英文:
Initialize if C++11 or higher:
QVector<QVector<int>> m_buffer=QVector<QVector<int>>(10);
Or in the constructor of m_buffer's class:
QVector<QVector<int>> m_buffer;
Constructor(): m_buffer(10){}
Best guess for this error is that compiler treats m_buffer
as function with return type QVector<QVector<int>>
as @Marek R mentioned.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论