Fake Expected a declaration



  • Hello. There is some test to make a function that return a pointer to matrix array
    Program bullding is succesful and it work succesful. A result in QTextEdit looks like this:
    1,2,3,4,5,6,7,8,
    9,10,11,12,13,14,15,16,
    17,18,19,20,21,22,23,24,
    25,26,27,28,29,30,31,32,

    But QT Creator say "Expected a declaration" (see screen http://imageshack.com/a/img673/9086/WodPUb.jpg)
    My questions:

    1. What a reason of that QT Creator say " Expected a declaration" and can not find the declaration in this function: BYTE (*MyProg::GetProgressiveMatrix(int n))[8] ?
    2. Is there some other way to return a pointer to matrix array BYTE WPlanMap[4][8];

    Code:
    //----------------------------------------------Header.h
    #ifndef Test_H
    #define Test_H
    #include "ui_USEROK.h"
    #include <QtGui>
    #include <QtCore>
    class MyProg : public QWidget, public Ui::MyProg
    {
    Q_OBJECT
    public:
    MyProg(QWidget* pwgt = 0, Qt::WindowFlags f = 0);
    BYTE (*GetProgressiveMatrix(int n))[8];
    public slots:
    void Deal3Start();
    protected:
    BYTE WPlanMap[4][8];
    };
    #endif

    //---------------------------------Source.h

    #include "Header.h"
    #include <cmath>
    #include <cstdlib>
    #include <cstdio>
    MyProg::MyProg(QWidget* pwgt,Qt::WindowFlags f) : QWidget(pwgt,f){
    setupUi(this);
    setFixedSize(700, 600);
    connect(Deal3Button, SIGNAL(clicked()), this, SLOT(Deal3Start()));
    }
    void MyProg::Deal3Start(){
    QString ResultStr = "";
    QString TempStr;
    BYTE (*pWPlanMap)[8];
    pWPlanMap = GetProgressiveMatrix(TestBox->value());

    for (int i = 0; i < 4; ++i){
        for (int j = 0; j < 8; ++j){
            ResultStr = ResultStr + TempStr.setNum(pWPlanMap[i][j]) + ",";
        }
        ResultStr = ResultStr + '\n';
    }
    TestResult->setText(ResultStr); //Result string
    

    }
    BYTE (MyProg::GetProgressiveMatrix(int n))[8] //Expected a declaration ???(see screen http://imageshack.com/a/img673/9086/WodPUb.jpg)
    {
    for (int i = 0; i < 4; ++i){
    for (int j = 0; j < 8; ++j){
    WPlanMap[i][j] = n+j+(i
    8);
    }
    }
    return WPlanMap;
    }


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    BYTE (*GetProgressiveMatrix(int n))[8]; is not a declaration. It's a something of a mix between a function and an array declaration.

    You can return a BYTE ** from your function. Depending on what you actually need that matrix for, Qt provides QGenericMatrix that you can use.



  • Thanks. It is interesting.
    For example I have matrix
    int Map[2][4] = {......};

    How to create a pointer to Map and return it. Can you code declaration of such function and create a pointer to Map that returned?
    and then print Map in loop


  • Qt Champions 2016

    returning multi-dim arrays from functions has ugly syntax.
    please see
    http://stackoverflow.com/questions/3716595/c-returning-multidimension-array-from-function

    something like

    #include <QDebug>
    
    const int MaxDim = 5;
    typedef int ( *myarray_return ) [MaxDim];
    myarray_return get_array()
    {
        static int local_myarray[MaxDim][MaxDim];
        for ( int i = 0; i < MaxDim; i ++ )
            for ( int h = 0; h < MaxDim; h++ )
            { local_myarray[i][h] = i * 10; }
    
        return local_myarray;
    
    }
    
    int main ( int argc, char* argv[] )
    {
       myarray_return ref_array = get_array();
        for ( int i = 0; i < MaxDim; i ++ )
            for ( int h = 0; h < MaxDim; h++ )
            { qDebug() << ref_array[i][h]; }
    
    }
    

    ps. sorry for no code formatting. Seems not be included in the editor anymore.

    [edit: Added missing coding tags: ``` before and after the code SGaist]



  • int **matrix;
    is a 2 dimensional matrix, pointer of pointers, or dynamically allocated array
    of pointers (dynamically allocated arrays).

    Init like:

    int width 5;
    int height = 5;
    
    int **matrix = new int*[height];
    for (int row = 0; row < height; ++row)
    {
        matrix[row] = new int[width];
    }
    
    

  • Lifetime Qt Champion

    There's a summary of the markdown tags used by the forum on the bottom of the page


  • Qt Champions 2016

    This post is deleted!

  • Qt Champions 2016

    @SGaist ahh thanks. I keep forgetting. When editor open, its not visible unless you scroll :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.