c++qtclassqvector

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


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);

// ...
}
  

Solution

  • 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.