what is the correct way to use vector of vectors to create class buffer? or how to pass it correctly through functions?

huangapple go评论54阅读模式
英文:

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&lt;QVector&lt;int&gt;&gt; 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&lt;QVector&lt;int&gt;&gt; m_buffer(10);
                                    ^~

and the same error for the function when I tried in the header :

public:
void getDataFromFiles(QList&lt;QString&gt; audioFilesList, QVector&lt;QVector&lt;int&gt; &gt; buffer(10));

But it compiles when I just keep void getDataFromFiles(QList&lt;QString&gt; audioFilesList, QVector&lt;QVector&lt;int&gt; &gt; 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&lt;QString&gt; audioFilesList, QVector&lt;QVector&lt;int&gt; &gt; buffer);

private:
    Ui::MainWindow *ui;

//  QVector&lt;QVector&lt;int&gt;&gt; buffer(10); // this does not compile   

};
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui-&gt;setupUi(this);

    QList&lt;QString&gt; audioFilesList;
    audioFilesList.append(&quot;/path/to/file1&quot;);
    audioFilesList.append(&quot;/path/to/file2&quot;);

    QVector&lt;QVector&lt;int&gt;&gt; buffer(10);

    getDataFromFiles(audioFilesList, buffer); // I think there is a way to pass buffer(10) instead?

}

void MainWindow::getDataFromFiles(QList&lt;QString&gt; audioFilesList, QVector&lt;QVector&lt;int&gt; &gt; 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&lt;QVector&lt;int&gt;&gt; m_buffer=QVector&lt;QVector&lt;int&gt;&gt;(10);

Or in the constructor of m_buffer's class:

    QVector&lt;QVector&lt;int&gt;&gt; m_buffer;
    Constructor(): m_buffer(10){}

Best guess for this error is that compiler treats m_buffer as function with return type QVector&lt;QVector&lt;int&gt;&gt; as @Marek R mentioned.

huangapple
  • 本文由 发表于 2023年2月6日 21:35:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75361997.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定